I have two objects... and if I compile a program with either one, it works fine, but when they both exist in the same program, I get the exception...
*"Entities in 'ObjectContext.UnitSet' participate in the 'Sheet_Statistics' relationship. 0 related 'Sheet' were found. 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
}
Then their configurations..
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.