我有两个对象...,如果我用任何一个编译程序,它可以正常工作,但是当它们都存在于同一个程序中时,我会得到异常。实体已经参与另一种关系,实体框架CodeOnly
“ObjectContext.UnitSet”中的实体参与“Sheet_Statistics”关系,找到0个相关的'Sheet',1'Sheet'is expected。
class Unit
{
public int Id;
public string Name;
}
class Template
{
public int Id;
public virtual ICollection<Unit> Units
}
class Sheet
{
public int Id;
public virtual ICollection<Unit> Units
}
然后它们的配置..
TemplateConfiguration:EntityConfiguration // ....
//// map the collection entity
HasMany(k => k.Units).WithRequired()
.Map("template.units",
(template, unit) => new
{
Template = template.Id,
Unit = unit.Id
});
SheetConfiguration:EntityConfiguration // ....
//// map the collection entity
HasMany(k => k.Units).WithRequired()
.Map("sheet.units",
(sheet, unit) => new
{
Sheet = sheet.Id,
Unit = unit.Id
});
UnitConfiguration : EntityConfiguration<Unit>
//
// Initialize the Primary Key
HasKey(k => k.Id);
// Initialize that the Key Increments as an Identity
Property(k => k.Id).IsIdentity();
var templates = new List<Template>
{
new Template
{
Name = // ..,
Units = new List<Unit>
{
new Unit
{
// ...
}
}
}
};
templates.ForEach(x =>
{
context.Templates.Add(x);
});
context.SaveChanges(); // <-- Exception Happens Here, I never even get to try to add Sheets.
哪里是实际抛出异常的代码? – 2010-09-01 21:37:11
当我尝试将模板添加到数据库并在Context上调用“SaveChanges()”时。 – Ciel 2010-09-01 22:25:46
我与克雷格在这一个,你需要提供更多的代码。这是诚实的所有映射代码? – TheCloudlessSky 2010-09-01 22:27:22