[英]EntityFramework Code First inheritance with custom discriminator
I'm trying to map the following inheritance in EntityFramework Code First 我正在尝试在EntityFramework Code First中映射以下继承
public class Member
{
public string ProjectName { get; set; }
public string AssemblyName { get; set; }
public string NamespaceName { get; set; }
public string TypeName { get; set; }
public string Signature { get; set; }
public string DisplayName { get; set; }
public MemberType Type { get; set; }
public string Summary { get; set; }
public bool IsStatic { get; set; }
public bool IsInherited { get; set; }
//public virtual Type ParentType { get; set; } // ignore this
}
public class Field : Member
{
public string ValueType { get; set; }
public bool IsReadOnly { get; set; }
public bool IsVolatile { get; set; }
}
public enum MemberType
{
Constructor,
Field,
Method,
Operator,
Property
}
As you can probably guess from the code, in time I plan to add more subclasses of Member (named Constructor, Method, Operator and Property), but first I'd just like to get the Field one to work properly. 您可能会从代码中猜到,我计划及时添加更多的Member子类(分别命名为Constructor,Method,Operator和Property),但首先,我想让Field正常工作。
My mapping-code looks like this 我的映射代码如下所示
modelBuilder.Entities<Member>().Configure(config =>
{
config.HasKey(m => new { m.ProjectName, m.AssemblyName, m.NamespaceName, m.TypeName, m.Signature });
config.Property(m => m.DisplayName).IsRequired();
config.ToTable("Members");
});
modelBuilder.Entities<Field>().Configure(config =>
{
config.HasKey(f => new { f.ProjectName, f.AssemblyName, f.NamespaceName, f.TypeName, f.Signature });
});
modelBuilder.Entity<Member>()
.Map<Field>(m =>
{
m.ToTable("Fields");
m.Requires("Type").HasValue((int)Service.DataTypes.MemberType.Field).IsRequired();
}); // note: realy not certain about this one
What I would like, is to generate two tables. 我想要的是生成两个表。 One named "Members", and one named "Fields", and use the Type
-property in the Member
-class as a discriminator, and I'm sure I'm just doing something really stupid here, but I cannot for the life of me figure out how to do it. 一个命名为“ Members”,一个命名为“ Fields”,并使用Member
-class中的Type
-property作为区分符,我敢肯定我只是在做些愚蠢的事情,但我不能终生我想办法。 Because with the current code, it generates a column in the "Fields" table named "Type", which you obviously wouldn't need as that'd be a constant. 因为使用当前代码,它会在“字段”表中生成一个名为“类型”的列,您显然不需要该列,因为那将是一个常量。
So I'm just wondering, could somebody please tell me what on earth I'm doing wrong? 所以我只是想知道,有人可以告诉我我做错了什么吗?
Is there a typo there ? 那里有错字吗? Your Class has a field Valuetype Not Type. 您的班级有一个值类型非类型字段。
I have never tried with with JUST 1 mapping, try with a second table to make sure. 我从未尝试过使用JUST 1映射,请尝试使用第二个表来确保。
modelBuilder.Entity<Member>()
.Map<Field>(m =>
{
m.ToTable("Fields");
m.Requires("ValueType").HasValue((int)Service.DataTypes.MemberType.Field).IsRequired();
})
.Map<SECONDTYPE>(m =>
{
m.Requires("ValueType").HasValue(42);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.