Map complex type per entity with EF 4.1 fluent API


  • Hello,

    suppose that I want to use fluent API for mapping to existing database. The database contains multiple tables which contain information about Address. I want to use single complex type Address but I need to map it per entity because address column names in tables are different. At the moment I'm able to map Address only globaly which is not what I want - I want the functionality similar to EDMX where complex type mapping was related to entity mapping. Is it possible?

    Best regards,

    Thursday, March 24, 2011 10:08 AM


  • Hi Ladislav,

    You can also customize the complex type columns names at the entity level with fluent API in EF 4.1. Here is an example:

    public class User
      public int UserId { get; set; }  
      public Address Address { get; set; }
    public class Customer
      public int CustomerId { get; set; }  
      public Address Address { get; set; }
    public class Address
      public string Street { get; set; }  
      public string City { get; set; }
    public class Context : DbContext
      public DbSet<User> Users { get; set; }
      public DbSet<Customer> Customers { get; set; }
      protected override void OnModelCreating(DbModelBuilder modelBuilder)
        modelBuilder.Entity<User>().Property(u => u.Address.Street)
        modelBuilder.Entity<Customer>().Property(u => u.Address.Street)

    You can find another example here.


    Hope this helps,


    • Proposed as answer by Morteza Manavi Thursday, March 24, 2011 2:59 PM
    • Marked as answer by Ladislav Mrnka Thursday, March 24, 2011 10:02 PM
    Thursday, March 24, 2011 2:58 PM