使用EF CTP5 TPH时出错 [英] Error when using EF CTP5 TPH

查看:97
本文介绍了使用EF CTP5 TPH时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用EF CTP5 TPH,如下所示:

I am trying to use EF CTP5 TPH as follows:

我有基类和两个继承的类,我使用'PropertyName'作为鉴别器:

I have base class and two inherited classes, and I use 'PropertyName' as discriminator:

   公共类DescriptionPropertyTemp

    {

        public int Id {get;组; }
        public string PropertyScope {get;组; }
        public string PropertyValue {get;组; }
        [NotMapped]

        public string PropertyName {get;组; }
   

    

        public virtual DescriptionSettings Descrption {get;组; }
    }




    public class DescriptionIncludeProperty:DescriptionPropertyTemp

    {

    }


    public class DescriptionExcludeProperty:DescriptionPropertyTemp

    {

      

    }

    public class DescriptionPropertyTemp
    {
        public int Id { get; set; }
        public string PropertyScope { get; set; }
        public string PropertyValue{ get; set; }
        [NotMapped]
        public string PropertyName { get; set; }
   
    
        public virtual DescriptionSettings Descrption { get; set; }
    }



    public class DescriptionIncludeProperty : DescriptionPropertyTemp
    {
    }

    public class DescriptionExcludeProperty : DescriptionPropertyTemp
    {
      
    }

CREATE TABLE [dbo]。[DescriptionPropertiesCollections](

    [Id] [ int] IDENTITY(1,1)NOT NULL,

    [DescriptionTypeCode] [varchar](50)NOT NULL,

     ; [PropertyName] [varchar](250)NOT NULL,

    [PropertyScope] [varchar](250)NOT NULL,

   ;  [PropertyValue] [varchar](4000)NOT NULL,

)ON [PRIMARY]

CREATE TABLE [dbo].[DescriptionPropertiesCollections](
    [Id] [int] IDENTITY(1,1) NOT NULL,
    [DescriptionTypeCode] [varchar](50) NOT NULL,
    [PropertyName] [varchar](250) NOT NULL,
    [PropertyScope] [varchar](250) NOT NULL,
    [PropertyValue] [varchar](4000) NOT NULL,
) ON [PRIMARY]

 

具有以下映射:

   public class DescriptionPropertiesTempConfig : EntityTypeConfiguration<DescriptionPropertyTemp>
{
public DescriptionPropertiesTempConfig()
{

this.ToTable("DescriptionPropertiesCollections");

this.HasRequired(descP => descP.Descrption)
.WithMany(desc => desc.DescriptionPropertyTemp)
.IsIndependent()
.Map(mc => mc.MapKey(desc => desc.Code, "DescriptionTypeCode"));

this.Map<DescriptionIncludeProperty>(d => d.Requires("PropertyName").HasValue("Include"));
this.Map<DescriptionExcludeProperty>(d => d.Requires("PropertyName").HasValue("Exclude"));

}
}

 

但是我收到以下错误:

  Schema指定的i无效。错误:

(52,12):错误2016:条件中指定的成员判别器不存在。

任何想法我为什么得到这个错误?

Any idea why I am getting this error?

 

 

此外 - 是否可以有两个不同的映射以上两个共享相同层次结构表的继承类:

Also - is it possible to have two different mappings for the above two inherited classes that share the same hierarchy table:

DescriptionIncludePropertiesTempConfig

 

DescriptionExcludePropertiesTempConfig

 

所以我最终可以获得每种类型的收藏?即使层次结构在一个表中?目前,当我尝试它时,我最终得到以下错误:

So I can end up with a collection per type? even though the hierarchy is in one table? currently when I tried it i ended up with the following error:

 

谢谢

Naomi

 

推荐答案

您首次例外的原因是您忘记指定鉴别器值基本类型(DescriptionPropertiesTempConfig)本身。  DescriptionPropertiesTempConfig 未定义为抽象类,因此可能存在此类型的
记录,这些记录也需要与TPH映射。因此,您需要将其设为抽象类或更改您的流畅API代码,如下所示:

The reason for your first exception is because you forgot to specify the discriminator value for the base type (DescriptionPropertiesTempConfig) itself. DescriptionPropertiesTempConfig is a not defined as an abstract class so there might be records of this type which also need to be mapped with TPH. Therefore, you need to either make it an abstract class or change your fluent API code like the following:


protected override void OnModelCreating(ModelBuilder modelBuilder)
{      
  modelBuilder.Entity<DescriptionPropertyTemp>()
        .Map<DescriptionPropertyTemp>(d => d.Requires("PropertyName").HasValue("Base"))
        .Map<DescriptionIncludeProperty>(d => d.Requires("PropertyName").HasValue("Include"))
        .Map<DescriptionExcludeProperty>(d => d.Requires("PropertyName").HasValue("Exclude"));
}


这篇关于使用EF CTP5 TPH时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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