EntityFramework: Is it possible to map two different columns to the same entity/model that follows the same practices?

c# entity-framework entity-framework-6


I'm building an application that takes data from an existing table, and then splits it into multiple entities, that follow specific trends. It is for a letting agent that manages multiple different agencies. One such shared entity that I have identified is the Address, shared by Landlord, Letting Agent and the actual property all of which share the properties below:

public class Address 
        public string HouseNumber
        public string FlatPosition
        public string AddressLine
        public string Town
        public string County
        public string Postcode

In the giant table, they follow the above schema, but are specified with the relevant noun in front e.g. "LandlordHouseNumber etc., PropertyHouseNumber etc., LettingAgentHouseNumber etc.,"

Is it possible to take the data from the table, and instantiate with the address class whilst being able to distinguish the entity they represent? Or will I need to have an address class for each?


Table heading structure

10/17/2019 9:03:00 AM

Accepted Answer

As suspected, what you want is to define a Complex Type for the address then map the properties to the appropriate columns.

public class Address 
    public string HouseNumber { get; set; }
    public string FlatPosition { get; set; }
    public string AddressLine { get; set; }
    public string Town { get; set; }
    public string County { get; set; }
    public string Postcode { get; set; }

public YourEntity
    public int Id { get; set; } // or whatever you use
    // other properties

    // complex type properites
    public Address LandlordAddress { get; set; }
    public Address LandlordAgentAddress { get; set; }

Then configure in your DbContext:

protected void OnModelCreating( DbModelBuilder modelBuilder )
    // configure like an entity but use .ComplexType<>()
        // e.g. set max size of one of the properties
        .Property( a => a.Postcode )
        .HasMaxLength( 10 );
    // etc.

    // map properties to columns if you don't want to use default naming convention
        .Property( yet => yet.LandlordAddress.Postcode )
        .HasColumnName( "LandlordPostcode" );

        .Property( yet => yet.LandordAgentAddress.Postcode )
        .HasColumnName( "LandlordAgentPostcode" );
10/17/2019 7:17:14 PM

Popular Answer

1. Fold in the database

You could create a view, or create a query, that folds these 3x columns into a sensible one column.

2. Fold upon retrieval

2a Linq-to-sql

public class Address 
        public string AHouseNumber 
        public string BHouseNumber 
        public string CHouseNumber 

You could do a simple concat:

await db.Addresses
  .Select( a=> new { Address = AHouseNumber + BHouseNumber + CHouseNumber } )

2b. Run custom query

Related Questions


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow