EF代码优先防止流利的API属性映射 [英] EF Code First prevent property mapping with Fluent API

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

问题描述

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

 公共类产品
{
公众的Guid标识{搞定;组; }

公共AddressDetails AddressDetails {搞定;组; }
}

公共类AddressDetails
{
公共字符串城{搞定;组; }
公共字符串国家{搞定;组; }
//其他属性
}



时,可以防止映射国家从 AddressDetails 产品类中的属性? (因为我永远不会需要它产品类)



像这样

 属性(p => p.AddressDetails.Country)结尾。可以忽略(); 


解决方案

对于EF5和老年人:
DbContext.OnModelCreating 覆盖为背景:

  modelBuilder.Entity<产品方式>()忽略(p => p.AddressDetails.Country); 



对于EF6:您的运气了。请参见 Mrchief的回答


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
}

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

Something like this

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

解决方案

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

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

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

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

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