1
我試圖使用實體框架的記錄添加到表中,我得到DbUpdateException
當我_entities.SaveChanges();
執行此方法:實體框架 - 當添加DbUpdateException實體
public Player CreatePlayer(int fideNumber, string name, string surname, DateTime birthDate, double? rating = null)
{
var player = _entities.Players.Add(new Player
{
Id = fideNumber,
BirthDate = new DateTime(birthDate.Year, birthDate.Month, birthDate.Day),
Name = name,
Surname = surname,
Rating = 1500
});
_entities.SaveChanges();
return player;
}
我Player
模式是這樣的:
public partial class Player
{
public Player()
{
this.Tournaments = new HashSet<Tournament>();
this.GamesAsWhite = new HashSet<Game>();
this.GamesAsBlack = new HashSet<Game>();
}
public int Id { get; set; }
public string Name { get; set; }
public string Surname { get; set; }
public System.DateTime BirthDate { get; set; }
public Nullable<double> Rating { get; set; }
public virtual ICollection<Tournament> Tournaments { get; set; }
public virtual ICollection<Game> GamesAsWhite { get; set; }
public virtual ICollection<Game> GamesAsBlack { get; set; }
}
我已經通過設計師創建了模型,然後生成了我的數據庫。以這種方式創建錦標賽的效果很好,但由於某些原因,玩家創建會引發錯誤。字段Id
的類型是Computed
。
由於您的'Id'字段是計算出來的(可能是Identity),您不應該爲其分配值。 –
出於某種原因,當我將我的字段設置爲Identity時,值將自動生成,但是當我將字段設置爲Identity時,值將是必需的... –
好吧,將Id字段StoreGeneratedPattern設置爲none無法解決問題。 –