0
我會嘗試使用EF和Fluent API創建一對一關係。EF一對一關係不會創建
第一類:
public class Game
{
public int Id { get; set; }
public Guid Token { get; set; }
public string Player { get; set; }
public virtual Field Field { get; set; }
public virtual ICollection<Move> Moves { get; set; }
public GameStatus Status { get; set; }
public DateTime StartTime { get; set; }
public DateTime? EndTime { get; set; }
public PlayerCode Winner { get; set; }
public Game()
{
Status = GameStatus.NoteDone;
StartTime = DateTime.UtcNow;
Winner = PlayerCode.None;
Field = new Field {Game = this};
Token = Guid.NewGuid();
}
}
Secong類:
public class Field : IEntity
{
public int Id { get; set; }
public virtual Game Game { get; set; }
public string CellsString { get; set; }
}
和配置方面
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Game>()
.HasRequired<Field>(g => g.Field)
.WithRequiredDependent(f => f.Game);
base.OnModelCreating(modelBuilder);
}
但這種關係在DB沒有創建後的關係。表看起來像這樣
我嘗試流利配置許多變化,但沒有人對我的作品。我在哪裏犯錯?
我已經問過在1-1所需的關係之前,它也使用表的主鍵作爲外鍵。你是否檢查過表格上的實際約束,而不僅僅是列? – Vlad274
@ Vlad274謝謝,我檢查我的約束,看到EF使用PK作爲FK –