c# - EF Code First 阻止使用 Fluent API 进行属性映射

标签 c# entity-framework ef-code-first fluent-interface

我有一个类 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
}

是否可以防止从 Product 类中的 AddressDetails 映射“Country”属性? (因为我永远不需要它用于 Product 类)

像这样

Property(p => p.AddressDetails.Country).Ignore();

最佳答案

对于 EF5 及更早版本:DbContext.OnModelCreating 覆盖您的上下文:

modelBuilder.Entity<Product>().Ignore(p => p.AddressDetails.Country);

对于 EF6:您真倒霉。参见 Mrchief's answer .

关于c# - EF Code First 阻止使用 Fluent API 进行属性映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15130814/

相关文章:

c# - 如何从 Entity Framework 元数据 EntityType 对象中获取相应的 POCO 对象类型?

c# - 如何使用正确的参数类型从 C# 正确调用 C++ DLL

c# - 仅当设置了属性时才应用样式

c# - Entity Framework 中 LINQ-to-SQL 的 ExecuteCommand 的等价物是什么?

c# - 将 Include() 调用替换为 Select()

postgresql - NPGSQL 日期时间数组

c# - EntityFramework 匿名复合键属性名称冲突

c# - 关于[纯]方法的问题

c# - 组合框上的数据绑定(bind)

entity-framework - 使用 Entity Framework 删除行