代码优先 - 将实体的字段映射到同一列 [英] Code-first - Mapping entities' fields to the same column

查看:79
本文介绍了代码优先 - 将实体的字段映射到同一列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Model-first和Table-per-heirachy,我可以创建两个继承自同一基类的类,并将两个派生类中的每一个中的列映射到同一个表列,这样我就可以'重复使用'列。

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.

如果我尝试使用Code-first,我会收到以下错误:"类型中的每个属性名称必须是唯一的。已经定义了属性名称"XXX"。"

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?

以下是一些示例代码:

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");
}
}

推荐答案

Hello Lee,

Hello Lee,

确实这看起来像个bug。我会把它归档并确保它不会丢失。谢谢你指出来了!

Indeed this looks like a bug. I'll file it and make sure it doesn't get lost. Thanks for pointing it out!


这篇关于代码优先 - 将实体的字段映射到同一列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆