映射实体字段到同一列
使用 Model-first 和 Table-per-heirachy,我可以创建从同一基类继承的两个类,并将两个派生类中每个派生类中的列映射到同一表列,以便我可以“重用”列。
如果我尝试使用“代码优先”,则会收到以下错误:“类型中的每个属性名称必须是唯一的。属性名称“XXX”已定义。”
我认为这是代码优先中的错误?
这是一些示例代码:
public class Parent
{
public Guid Id { get; set; }
}
public class ChildA : Parent
{
public Int32 ChildAProperty { get; set; }
}
public class ChildB : Parent
{
public Int32 ChildBProperty { get; set; }
}
public class TestContext : DbContext
{
public DbSet<Parent> Entities { get { return this.Set<Parent>(); } }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
var childAConfig = modelBuilder.Entity<ChildA>();
childAConfig.Property(p => p.ChildAProperty).HasColumnName("Property");
var childBConfig = modelBuilder.Entity<ChildB>();
childBConfig.Property(p => p.ChildBProperty).HasColumnName("Property");
}
}
Using Model-first and Table-per-heirachy, I can create two classes that inherit from the same base class, and map a column in each of the two derived classes to the same table column so that I can 're-use' columns.
If I try that with Code-first, I get the following error: "Each property name in a type must be unique. Property name 'XXX' was already defined."
I assume that this is a bug in code-first?
Here is some example code:
public class Parent
{
public Guid Id { get; set; }
}
public class ChildA : Parent
{
public Int32 ChildAProperty { get; set; }
}
public class ChildB : Parent
{
public Int32 ChildBProperty { get; set; }
}
public class TestContext : DbContext
{
public DbSet<Parent> Entities { get { return this.Set<Parent>(); } }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
var childAConfig = modelBuilder.Entity<ChildA>();
childAConfig.Property(p => p.ChildAProperty).HasColumnName("Property");
var childBConfig = modelBuilder.Entity<ChildB>();
childBConfig.Property(p => p.ChildBProperty).HasColumnName("Property");
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论