我試圖用這種種子方法;如何使用種子方法爲我的上下文創建初始數據?
context.Reeves.AddOrUpdate(
p => new { p.FirstName, p.LastName },
new Reeve { FirstName = "A", LastName = "A" },
new Reeve { FirstName = "B", LastName = "B" });
context.SaveChanges();
context.Districts.AddOrUpdate(
p => p.Name,
new District() { Name = "X", ReeveId = context.Reeves.First(r => r.FirstName == "A" && r.LastName == "A").Id },
new District() { Name = "Y", ReeveId = context.Reeves.First(r => r.FirstName == "B" && r.LastName == "B").Id });
context.SaveChanges();
我收到錯誤消息「的INSERT語句衝突與外鍵約束‘FK_dbo.District_dbo.Reeve_Id’。該衝突發生於數據庫‘ProjectTracking’,表‘dbo.Reeve’,列「 ID'。」
如果我更改下面的代碼;
context.Districts.AddOrUpdate(
p => p.Name,
new District() { Name = "X", Reeve = context.Reeves.First(r => r.FirstName == "A" && r.LastName == "A") },
new District() { Name = "Y", Reeve = context.Reeves.First(r => r.FirstName == "B" && r.LastName == "B") });
context.SaveChanges();
錯誤消息disapear但是當我檢查各區表我看到所有ReeveId列均爲0
什麼是我的錯誤,任何想法?
PS:我不想創建內聯Reeve的區內的AddOrUpdate方法。就像是; context.Districts.AddOrUpdate(p => p.Name,new District(){Name =「X」,Reeve = new Reeve(){FirstName =「A」,LastName =「A」});
我的實體
public class Reeve
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string FullName
{
get { return string.Format("{0} {1}", FirstName, LastName); }
}
public virtual District District { get; set; }
public byte[] RowVersion { get; set; }
}
public class District
{
public District()
{
Projects = new HashSet<Project>();
ProjectRequests = new HashSet<ProjectRequest>();
}
public int Id { get; set; }
public string Name { get; set; }
public int ReeveId { get; set; }
public virtual Reeve Reeve { get; set; }
public byte[] RowVersion { get; set; }
public virtual ICollection<Project> Projects { get; set; }
public virtual ICollection<ProjectRequest> ProjectRequests { get; set; }
}
實體配置
public class ReeveConfiguration : EntityTypeConfiguration<Reeve>
{
public ReeveConfiguration()
{
HasKey<int>(p => p.Id);
Ignore(p => p.FullName);
Property(p => p.FirstName).HasColumnAnnotation("Index", new IndexAnnotation(new IndexAttribute("FullName", 1) { IsUnique = true })).HasMaxLength(50).IsRequired();
Property(p => p.LastName).HasColumnAnnotation("Index", new IndexAnnotation(new IndexAttribute("FullName", 2) { IsUnique = true })).HasMaxLength(50).IsRequired();
Property(p => p.RowVersion).IsRowVersion();
}
}
public class DistrictConfiguration : EntityTypeConfiguration<District>
{
public DistrictConfiguration()
{
HasKey<int>(p => p.Id);
HasRequired(p => p.Reeve).WithOptional(p => p.District);
HasMany(p => p.Projects).WithRequired(p => p.District).HasForeignKey(p => p.DistrictId);
HasMany(p => p.ProjectRequests).WithRequired(p => p.District).HasForeignKey(p => p.DistrictId);
Property(p => p.Name).HasColumnAnnotation("Index", new IndexAnnotation(new IndexAttribute() { IsUnique = true })).HasMaxLength(100).IsRequired();
Property(p => p.RowVersion).IsRowVersion();
}
}
我恨實體框架球隊的1對1級的關係的規則。委託實體必須與原則實體具有相同的名稱PK,並且委託實體PK也必須是FK。這一定是個笑話。所有1對1的關係不能像Person - > PersonPhoto或Car - >方向盤。我是對的還是我誤解了他們的邏輯。例如,我也有項目和項目請求實體,項目的請求和請求的項目可以爲空我的意思是他們有0..1到0..1的關係,他們必須是自己的PK ID。還有,如果我有實體基類有Id主鍵字段。我如何從中導出我的1對1關係實體。
你的模特是什麼樣的?你的錯誤提到Reeve_Id,那麼你說ReeveId列是零。如果你在區內同時擁有ReeveId和Reeve_Id,那麼你的關係是不正確的。 –
我使用實體及其配置更新了我的帖子。另外,如果我的1對1關係像你說的那樣是錯誤的,那麼我想分享我的其他實體,它們是項目和projectrequest,它們的關係是0..1到0..1。 –