我有相当复杂的 EF 映射现实场景,它似乎只有在我打破封装时才有效。也许有人会弄清楚如何正确地做到这一点,或者我们会接受这是一个错误。
我有 2 个项目,我们称它们Domain
为Persistance
. 包含 POCO 类,并通过属性Domain
公开其内部。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);
}
}
对于某些产品:
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
迁移,则会创建。这就是问题所在。我不想暴露那必须是私有财产。
因为AnotherSomePropInternal
一切正常internal
。