【发布时间】:2016-07-12 16:26:47
【问题描述】:
我首先使用实体框架代码来设计一个 SQL 数据库和“SEED”方法来用初始数据填充数据库。以下是两个具有一对多关系的模型。 “Foo”可以有多个“FooSection”
public class Foo {
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int FooId {get; set;}
// Some more properties.
// Navigation collection
public virtual ICollection<FooSection> fooSections {get; set;}
}
public class FooSection {
// Has composite key
[ForeignKey("foo"), Column(Order=1)]
public int FooId {get; set;}
[Key, Column(Order=2)]
public string SectionName {get; set;}
// Some more properties
// Navigation property
public virtual Foo foo {get; set;}
}
假设 FooId = 1 的“Foo”的第一个实例有 2 个“FooSection”,而 FooId = 2 的“Foo”的第二个实例有 1 个“FooSection”。所以我的种子方法看起来像这样-
protected override void Seed(PDI.Cloud.Models.ApplicationDbContext context)
{
// First add data for "Foo"
var FooList = new List<Foo>();
if(!(context.Foos.Any()))
{
// First instance of Foo
FooList.Add(
new Foo{
// Assign values to properties here
}
);
// Second instance of Foo
FooList.Add(
new Foo{
// Assign values to properties here
}
);
FooList.ForEach(f => context.Foos.AddOrUpdate(f));
context.SaveChanges();
// Get info of "FooSection"
var fooSectList = getFooSectList(context);
// Assign "FooSection"s to respective "Foo".
FooList[0].fooSections.Add(fooSectList[0]); // Section 1 for Foo with id = 1
FooList[0].fooSections.Add(fooSectList[1]); // Section 2 for Foo with id = 1
FooList[1].fooSections.Add(fooSectList[2]); // Section 1 for Foo with id = 2
Context.SaveChanges();
}
}
private List<FooSection>getFooSectList(PDI.Cloud.Models.ApplicationDbContext context)
{
var FooSectList = new List<FooSection>();
if(!(context.FooSections.Any()))
{
// 1st FooSection for Foo with FooId = 1
FooSectList.Add(
new FooSection{
FooId = 1,
SectionName = "Sect1"
}
);
// 2nd FooSection for Foo with FooId = 1
FooSectList.Add(
new FooSection{
FooId = 1,
SectionName = "Sect2"
}
);
// 1st FooSection for Foo with FooId = 2
FooSectList.Add(
new FooSection{
FooId = 2,
SectionName = "Sect1"
}
);
FooSectList.ForEach(f => context.FooSections.AddOrUpdate(f));
context.SaveChanges();
}
return FooSectList;
}
当我尝试运行种子方法时,它给了我 SQLException“违反 PRIMARY KEY 约束。无法在对象中插入重复键。重复键值为 (0, Sect1)”
我在这里遗漏了什么吗?因为我认为复合键只要组合是唯一的,我不应该得到这样的错误。
我将不胜感激。
谢谢。
【问题讨论】:
标签: entity-framework composite-key