Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EF Code First prevent property mapping with Fluent API

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(); 
like image 392
Catalin Avatar asked Feb 28 '13 08:02

Catalin


People also ask

Does EF core support fluent API?

In Entity Framework Core, the ModelBuilder class acts as a Fluent API. By using it, we can configure many different things, as it provides more configuration options than data annotation attributes.

How do you configure a primary key for an entity in Entity Framework fluent API?

Configuring a primary key By convention, a property named Id or <type name>Id will be configured as the primary key of an entity. Owned entity types use different rules to define keys. You can configure a single property to be the primary key of an entity as follows: Data Annotations.

Which is a fluent API method that can be used to configure a default value for the column that the property maps to?

Default Schema You can use the HasDefaultSchema method on DbModelBuilder to specify the database schema to use for all tables, stored procedures, etc.


1 Answers

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.

like image 187
Twon-ha Avatar answered Sep 23 '22 11:09

Twon-ha