实体框架:从通用基础类型映射内部属性

时间:2012-10-09 10:45:01

标签: entity-framework generics database-migration internalsvisibleto

我有相当复杂的EF Mapping现实场景,这似乎只有在打破封装时才有用。也许有人会清楚它是如何正确地做到的,或者我们会接受它是一个错误。

我有两个项目,我们称之为DomainPersistanceDomain包含POCO类,并通过Persistance属性将其内部公开给InternalsVisibleTo

Domain内(目的是坚持SomeProduct):

pubic class Company { ... }
pubic class InsuranceCompany : Company { ... }

pubic class Product<TCompany> where TCompany : Company
{
    ...
    public CustomValueType SomeProp { ... 
      // get/set from/into SomePropInternal 
    }
    internal string SomePropInternal { get;set; } 
    // exposed as internal to be accessible from EF mappings
}

public class SomeProduct : Product<InsuranceCompany> 
{
    ...
    public AnotherCustomValueType AnotherSomeProp { ... 
      // get/set from/into AnotherSomePropInternal 
    }
    internal string AnotherSomePropInternal { get;set; } 
    // exposed as internal to be accessible from EF mappings
}

我需要以一个表SomeProduct结束所有字段。所以,在Persistance我们有:

Persistance

所有Products的基本配置类型:

public class ProductEntityTypeConfiguration<TProduct, TCompany> : EntityTypeConfiguration<TProduct> 
    where TProduct : Product<TCompany> 
    where TCompany : Organization
{
    public ProductEntityTypeConfiguration() 
    {
        ...
        Property(_ => _.SomePropInternal)
            .IsRequired()
            .IsUnicode()
            .HasMaxLength(100);
    }
}

对于SomeProduct:

public class SomeProductMap : ProductEntityTypeConfiguration<SomeProduct, InsuranceCompany>
{
    public SomeProductMap()
    {
        ToTable("Business.SomeProduct");

        Property(_ => _.AnotherSomePropInternal)
            .IsRequired()
            .IsUnicode()
            .HasMaxLength(100);
    }
}

当我尝试使用这些映射添加迁移时,它会显示SomePropInternal is not declared on the type SomeProduct

如果我将internal string SomePropInternal更改为public string SomePropInternal,则会创建迁移。这就是问题所在。我不想暴露那个必须私有的财产。

对于AnotherSomePropInternalinternal一切正常。

0 个答案:

没有答案