我在实体框架的仅代码模型中的“基本类型”遇到了很多麻烦。我在实体框架的仅代码模型中的“基本类型”遇到了很多麻烦。
当我尝试使用 aDbContext
和 a运行此代码时DbSet<Template>
,出现以下错误。
A 导航属性“Flags”映射到两个不同的连接表“page.flags”和“template.flags”。导航属性可能只存在一个映射
这对我说的是我无法映射继承的属性。这对许多面向对象的代码设计来说是相当大的突破。有已知的补救措施吗?我意识到我可以使 Layout 成为非抽象的,并且有支持,但很明显这不是领域模型的意图。抽象类是基础,而不是存储模型。
我想补充一点,如果我把它IList<Flag>
放在Template
类中,这段代码就会运行。Id
即使通过继承,该领域仍然有效。我不明白为什么会这样。有人可以启发我吗?
public abstract class Layout
{
public virtual int Id
{
get;
set;
}
public virtual IList<Flag> Flags
{
get;
set;
}
}
public class Template : Layout
{
public virtual string Name
{
get;
set;
}
}
public class Page: Layout
{
}
public class LayoutConfiguration : EntityConfiguration<Layout>
{
public LayoutConfiguration()
{
HasKey(u => u.Id);
Property(u => u.Id).IsIdentity();
MapHierarchy().Case<Page>(c => new
{
c.Id
}).ToTable("Pages");
MapHierarchy().Case<Template>(c => new
{
c.Id,
c.Name
}).ToTable("Templates");
}
}
public class TemplateConfiguration : EntityConfiguration<Template>
{
public TemplateConfiguration()
{
Property(o => o.Name).HasMaxLength(64).IsUnicode();
HasMany(u => u.Flags).WithOptional()
.Map("template.flags",
(template, flag) => new {
Template = template.Id,
Flag = flag.Id
});
MapSingleType(c => new {
c.Id,
c.Name
}).ToTable("templates");
}
}
public class PageConfiguration : EntityConfiguration<Page>
{
public PageConfiguration()
{
HasMany(c => c.Flags).WithOptional()
.Map("page.flags",
(page, flag) => new
{
Page = page.Id,
Flag = flag.Id
});
}
}