EF Code First prevent property mapping with Fluent API
Asked Answered
R

7

33

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();
Rexfourd answered 28/2, 2013 at 8:30 Comment(12)
Have you overridden DbContext.OnModelCreating yet? Does modelBuilder.Entity<Product>().Ignore(p => p.AddressDetails.Country) in that method fail?Gerhart
Yes, exactly, but Ignore() method doesn't exist.Rexfourd
It does for me when using Entity Framework 4.4.Gerhart
I am using EF5 and it's missing..strange..it has been replaced?Rexfourd
It is working..sorry..i was starting with Property(p => p.PropertyName).Ignore() and i should have started with Ignore(p => p.PropertyName)Rexfourd
Cool, I've posted that as an answer.Gerhart
Have you considered using a different class for Product.AddressDetail. It obviously has a different behavior from the AddressDetail used elsewhere.Braid
@Aron: AddressDetail is a Complex Type and is made to be shared across. A solution would be to create a base class AddressDetail and then create two new classes that inherits from it: UserAddress and ProductAddress but i was trying to avoid this.Rexfourd
@RaraituL You mean you were trying to avoid SOLID design? en.wikipedia.org/wiki/Liskov_substitution_principleBraid
@Braid Not necessarily. In my case i wanted to avoid creating a new business object for one property that is not used. Probably if the ignored properties will grow and/or methods will be different, then i would consider moving them into separate entities, of-course. i could say i am trying to use KISS now :)Rexfourd
@RaraituL the ignore code, if it worked, would break LSP. If you took a function that assumes that Country != null and run it against Product.AddressDetail it would throw. Therefore LSP was violated. PS. Less classes does not equal simple. In your case you have created a new edge case, which is not KISS.Braid
I got your point. If somebody would see the Country field in Product's Address, would probably think that the value actually exists and will get an exceptionRexfourd
G
31

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.

Gerhart answered 28/2, 2013 at 10:50 Comment(4)
Doesn't work: The expression 'p => p.AddressDetails.Country' is not a valid property expression. The expression should represent a property: C#: 't => t.MyProperty' VB.Net: 'Function(t) t.MyProperty'.Ferous
@Ferous Right, this was for older versions. I've added a link to your answer, since that is the correct one for current EF versions.Gerhart
Sad that they would actively prevent this in EF6. Hope it was for some hidden good reason.Accommodative
This is really annoying. It seems they've removed functionality.Regurgitate
F
15

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.

Ferous answered 30/7, 2014 at 2:11 Comment(3)
What do you mean with if the child class is not an entity? If the child class is a ComplexType?Rexfourd
Yeah. If it is an entity, you can add its own configuration and skip the mapping.Ferous
@Ferous you can try modelBuilder.ComplexType<AddressDetails>().Ignore(ad => ad.Country); to achieve the same result using Fluent API.Pederasty
T
11

If you are using an implementation of EntityTypeConfiguration you can use the Ignore Method:

public class SubscriptionMap: EntityTypeConfiguration<Subscription>
{
    // Primary Key
    HasKey(p => p.Id)

    Property(p => p.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
    Property(p => p.SubscriptionNumber).IsOptional().HasMaxLength(20);
    ...
    ...

    Ignore(p => p.SubscriberSignature);

    ToTable("Subscriptions");
}
Tarahtaran answered 13/1, 2015 at 15:30 Comment(2)
What do you mean it does not work with complex types ? Mapping complex types is done automatically when you add the virtual keyword in your property declaration. If you don't want to map it, simply remove the virtual keyword.Tempered
Oh.. you mean when in a nested object... I understand.Tempered
T
4

While I realize that this is an old question, the answers didn't resolve my issue with EF 6.

For EF 6 you need to create a ComplexTypeConfiguration Mapping.

example:

public class Workload
{
    public int Id { get; set; }
    public int ContractId { get; set; }
    public WorkloadStatus Status {get; set; }
    public Configruation Configuration { get; set; }
}
public class Configuration
{
    public int Timeout { get; set; }
    public bool SaveResults { get; set; }
    public int UnmappedProperty { get; set; }
}

public class WorkloadMap : System.Data.Entity.ModelConfiguration.EntityTypeConfiguration<Workload>
{
    public WorkloadMap()
    {
         ToTable("Workload");
         HasKey(x => x.Id);
    }
}
// Here This is where we mange the Configuration
public class ConfigurationMap : ComplexTypeConfiguration<Configuration>
{
    ConfigurationMap()
    {
       Property(x => x.TimeOut).HasColumnName("TimeOut");
       Ignore(x => x.UnmappedProperty);
    }
}

If your Context is loading configurations manually you need to add the new ComplexMap, if your using the FromAssembly overload it'll be picked up with the rest of the configuration objects.

Telemachus answered 25/5, 2016 at 15:41 Comment(0)
D
2

On EF6 you can configure the complex type:

 modelBuilder.Types<AddressDetails>()
     .Configure(c => c.Ignore(p => p.Country))

That way the property Country will be always ignored.

Dahl answered 4/6, 2015 at 16:23 Comment(0)
T
1

Try this

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

It worked for me in similar case.

Twofold answered 29/3, 2016 at 16:57 Comment(0)
E
-2

It can be done in Fluent API as well, just add in the mapping the following code

this.Ignore(t => t.Country), tested in EF6

Eductive answered 7/1, 2015 at 8:29 Comment(1)
The question is about ignoring a property on a complex type. The precise scenario that won't work under EF6.Accommodative

© 2022 - 2024 — McMap. All rights reserved.