【发布时间】:2013-02-14 08:45:59
【问题描述】:
我有一个类 Product 和一个复杂类型 AddressDetails
public class Product
{
public Guid Id { get; set; }
public AddressDetails AddressDetails { get; set; }
}
public class AddressDetails
{
public string City { get; set; }
public string Country { get; set; }
// other properties
}
是否可以防止在 AddressDetails 内映射“国家”属性 Product 类? (因为Product 类我永远不需要它)
类似的东西
Property(p => p.AddressDetails.Country).Ignore();
【问题讨论】:
-
您是否覆盖了
DbContext.OnModelCreating?那个方法中的modelBuilder.Entity<Product>().Ignore(p => p.AddressDetails.Country)会失败吗? -
是的,没错,但是 Ignore() 方法不存在。
-
您是否考虑过为
Product.AddressDetail使用不同的类。它显然与其他地方使用的AddressDetail有不同的行为。 -
@RaraituL 你的意思是你试图避免 SOLID 设计? en.wikipedia.org/wiki/Liskov_substitution_principle
-
@RaraituL 忽略代码,如果有效,将破坏 LSP。如果您采用一个假定
Country != null的函数并针对Product.AddressDetail运行它,它将抛出。因此违反了 LSP。 PS。更少的类不等于简单。在您的情况下,您创建了一个新的边缘情况,这不是 KISS。
标签: c# entity-framework ef-code-first fluent-interface