EF 4.1的多重继承问题 [英] Multiple Inheritance issue with EF 4.1

查看:64
本文介绍了EF 4.1的多重继承问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不确定我在这里做错了什么,但是我在多重继承和构建模型方面遇到了问题.我收到错误消息"属性'Id'不是类型上已声明的属性... ".在添加ContextEntity类和TPC模型之前,一切工作正常.每个(非抽象)派生的实体都有自己的ID和自己的表.其他类始终存在,并且我的映射工作正常.这是我的课程:

I am not sure what I am doing wrong here, but I am having an issue with multiple inheritance and building my model. I get the error "The property 'Id' is not a declared property on type...". Everything worked fine before I added the ContextEntity class and I had a TPC model. Each (non abstract)derived entity had it's own ID and own table. The other classes always existed and my mappings worked fine. Here are my classes:

public abstract class Entity
    {
        public virtual Guid Id { get; set; } 
        public DateTime DateCreated { get; set; }
        public DateTime DateModified { get; set; }
        public EntityStatus EntityStatus { get; set; }
        public byte[] RowVersion { get; set; }
    }


    public abstract class ContextEntity : Entity
    {
        public string Description { get; set; }
        public ICollection<Comment> Comments { get; set; }
        public virtual Contact Owner { get; set; }
    }


    public abstract class Document : ContextEntity
    {

        public virtual Subscription Subscription { get; set; }

    }

    //This is the Class I want as a table
    public class Rfi : Document
    {
        public string Number { get; set; }
        public string Subject { get; set; }

    }

在拥有ContextEntity之前,我只有Entity.并非我所有的实体都将使用ContextEntity.我有这个映射文件:

Before I had the ContextEntity I only had Entity. Not all my entities will use the ContextEntity. I have this mapping file:

public class EntityConfiguration<TEntity> : EntityTypeConfiguration<TEntity>
    where TEntity : Entity
    {
        protected EntityConfiguration()
        {
            HasKey(e => e.Id);

            Property(e => e.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
            Property(e => e.RowVersion).IsRowVersion();
        }
    }

当我刚有了Entity基本类型时,它的效果很好.所以我想我会添加另一个这样的配置映射器:

When I just had the Entity base type it worked great. So I thought I would add another configuration mapper like this:

public class ContextEntityConfiguration<TEntity> : EntityTypeConfiguration<TEntity>
        where TEntity : ContextEntity
    {
        protected BridgeEntityConfiguration()
        {

            HasKey(e => e.Id);

            Property(e => e.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
            Property(e => e.RowVersion).IsRowVersion();
            HasMany(e => e.Comments).WithMany().Map(m =>
                                                        {
                                                            m.MapLeftKey("CommentId");
                                                            m.MapRightKey("EntityId");
                                                            m.ToTable("Entity_Comments");
                                                        });
            HasMany(e => e.Attachments).WithMany().Map(m =>
                                                           {
                                                               m.MapLeftKey("AttachmentId");
                                                               m.MapRightKey("EntityId");
                                                               m.ToTable("Entity_Attachments");
                                                           });
        }
    }

我的Derive映射类如下:

My Derive mapping class looks like this:

RfiMapping: ContextEntityConfiguration<Rfi>

我猜测EF不知道该如何处理所有嵌套基类?

I am guessing EF doesn't know what to do with all the nested base classes?

推荐答案

问题出在我的Document类中的Subscription属性.在该类中是一个需要为空的属性.一旦添加了Subscription类的映射并更新了null属性,一切工作就很好了.不知道为什么在我的Rfi课堂上给我一个错误,但是问题不在那个课堂上.

The issue was with the Subscription property in my Document class. In that class was a property that needed to be null-able. Once I added the mapping for the Subscription class and updated the null able property everything worked fine. Not sure why it was giving me an error on my Rfi class, but the issue was not in that class.

这篇关于EF 4.1的多重继承问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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