EF Code First prevent property mapping with Fluent API

后端 未结 7 1244
鱼传尺愫
鱼传尺愫 2020-12-29 20:42

I have a class Product and a complex type AddressDetails

public class Product
{
    public Guid Id { get; set; }

    public Addres         


        
7条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-29 21:09

    Unfortunately the accepted answer doesn't work, not at least with EF6 and especially if the child class is not an entity.

    I haven't found any way to do this via fluent API. The only way it works is via data annotations:

    public class AddressDetails
    {
        public string City { get; set; }
    
        [NotMapped]
        public string Country { get; set; }
        // other properties
    }
    

    Note: If you have a situation where Country should be excluded only when it is part of certain other entity, then you're out of luck with this approach.

提交回复
热议问题