32

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();
Catalin
  • 11,503
  • 19
  • 74
  • 147
  • 2
    Have you overridden `DbContext.OnModelCreating` yet? Does `modelBuilder.Entity().Ignore(p => p.AddressDetails.Country)` in that method fail? – Twon-ha Feb 28 '13 at 08:59
  • Yes, exactly, but Ignore() method doesn't exist. – Catalin Feb 28 '13 at 10:16
  • It does for me when using Entity Framework 4.4. – Twon-ha Feb 28 '13 at 10:20
  • I am using EF5 and it's missing..strange..it has been replaced? – Catalin Feb 28 '13 at 10:39
  • It is working..sorry..i was starting with Property(p => p.PropertyName).Ignore() and i should have started with Ignore(p => p.PropertyName) – Catalin Feb 28 '13 at 10:40
  • Cool, I've posted that as an answer. – Twon-ha Feb 28 '13 at 10:50
  • 1
    Have you considered using a different class for `Product.AddressDetail`. It obviously has a different behavior from the `AddressDetail` used elsewhere. – Aron Jul 30 '14 at 06:49
  • @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. – Catalin Jul 30 '14 at 07:38
  • 1
    @RaraituL You mean you were trying to avoid SOLID design? http://en.wikipedia.org/wiki/Liskov_substitution_principle – Aron Jul 30 '14 at 07:48
  • @Aron 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](http://en.wikipedia.org/wiki/KISS_principle) now :) – Catalin Jul 30 '14 at 08:27
  • 1
    @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. – Aron Jul 30 '14 at 08:31
  • 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 exception – Catalin Jul 31 '14 at 06:18

7 Answers7

30

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.

Community
  • 1
  • 1
Twon-ha
  • 944
  • 7
  • 21
  • 3
    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'.` – Mrchief Jul 30 '14 at 02:06
  • 1
    @Mrchief Right, this was for older versions. I've added a link to your answer, since that is the correct one for current EF versions. – Twon-ha Apr 14 '15 at 11:24
  • 1
    Sad that they would actively prevent this in EF6. Hope it was for some hidden good reason. – Søren Boisen Apr 29 '15 at 18:43
  • This is really annoying. It seems they've removed functionality. – Jeremy Holovacs Nov 10 '15 at 18:01
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.

Mrchief
  • 75,126
  • 20
  • 142
  • 189
  • What do you mean with `if the child class is not an entity`? If the child class is a `ComplexType`? – Catalin Jul 30 '14 at 07:39
  • Yeah. If it is an entity, you can add its own configuration and skip the mapping. – Mrchief Jul 30 '14 at 15:56
  • @Mrchief you can try modelBuilder.ComplexType().Ignore(ad => ad.Country); to achieve the same result using Fluent API. – Verbon Oct 23 '17 at 13:47
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");
}
JAVizcaino
  • 119
  • 1
  • 3
  • 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. – Maxime Aug 03 '15 at 20:37
  • Oh.. you mean when in a nested object... I understand. – Maxime Aug 03 '15 at 20:39
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.

Salizar Marxx
  • 917
  • 6
  • 11
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.

davidfcruz
  • 29
  • 2
1

Try this

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

It worked for me in similar case.

Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
Jan
  • 19
  • 2
-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