Entity framework EF4 code first fluent映射不适用于继承的属性?

Entity framework EF4 code first fluent映射不适用于继承的属性?,entity-framework,entity-framework-4,ef-code-first,code-first,invalidoperationexception,Entity Framework,Entity Framework 4,Ef Code First,Code First,Invalidoperationexception,有人试过映射继承的属性吗?因为我很高兴听到这样的说法,我在某个地方犯了一个错误,因为我得到了以下错误: “属性‘UserName’不是类型‘Advertiser’上声明的属性。请验证该属性未使用Ignore方法或NotMappeAttribute数据批注从模型中明确排除。请确保它是有效的基元属性。” 我的模型如下所示: abstract class Entity { public int Id {get; set; }} abstract class User : Entity { public

有人试过映射继承的属性吗?因为我很高兴听到这样的说法,我在某个地方犯了一个错误,因为我得到了以下错误:

“属性‘UserName’不是类型‘Advertiser’上声明的属性。请验证该属性未使用Ignore方法或NotMappeAttribute数据批注从模型中明确排除。请确保它是有效的基元属性。”

我的模型如下所示:

abstract class Entity { public int Id {get; set; }}
abstract class User : Entity { public string UserName {get; set;} }
sealed class Advertiser : User { }
class AdvertiserConfiguration : EntityTypeConfiguration<Advertiser>
{
   public AdvertiserConfiguration()
   {
      // the following line indirectly causes an InvalidOperationException:
      Property( x => x.UserName ).HasMaxLength(50);
   }
}
我的AdvertisementConfiguration类如下所示:

abstract class Entity { public int Id {get; set; }}
abstract class User : Entity { public string UserName {get; set;} }
sealed class Advertiser : User { }
class AdvertiserConfiguration : EntityTypeConfiguration<Advertiser>
{
   public AdvertiserConfiguration()
   {
      // the following line indirectly causes an InvalidOperationException:
      Property( x => x.UserName ).HasMaxLength(50);
   }
}
类广告客户配置:EntityTypeConfiguration
{
公共广告商配置()
{
//以下行间接导致InvalidOperationException:
属性(x=>x.UserName);
}
}
如果我更改广告客户类,使其不从用户继承(并下拉UserName属性),则一切正常。

您可以(在本例中必须)定义抽象类型的映射:

class UserConfiguration : EntityTypeConfiguration<User>
{
    public UserConfiguration()
    {
        Property( x => x.UserName ).HasMaxLength(50);
    }
}
用户
是一个实体-抽象,但仍然是一个具有所有映射选项的实体。

您可以(在本例中必须)为抽象类型定义映射:

class UserConfiguration : EntityTypeConfiguration<User>
{
    public UserConfiguration()
    {
        Property( x => x.UserName ).HasMaxLength(50);
    }
}

用户
是一个实体-抽象,但仍然是一个具有所有映射选项的实体。

真棒,这就像一个魅力!如果可能的话,我会给你更多的分数:)谢谢你拯救了这一天太棒了,真是魅力无穷!如果可能的话,我会给你更多的分数:)谢谢你挽救了这一天