EF代码首先防止使用Fluent API进行属性映射 [英] EF Code First prevent property mapping with Fluent API

查看:96
本文介绍了EF代码首先防止使用Fluent API进行属性映射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类产品和复杂类型 AddressDetails

I have a class Product and a complex type 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 里面产品类? (因为我不会需要它产品类)

Is it possible to prevent mapping "Country" property from AddressDetails inside Product class? (because i will never need it for Product class)

这样的一个

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


推荐答案

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

For EF5 and older: In the DbContext.OnModelCreating override for your context:

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

对于EF6:你没有运气。请参阅 Mrchief的回答

For EF6: You're out of luck. See Mrchief's answer.

这篇关于EF代码首先防止使用Fluent API进行属性映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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