0

I'm converting an existing EF model to use code first.

It has a number of Complex Types, that have a specific db field naming convention:

<propertyName>_<complexTypeName>_<complexTypeProperty>

So for example, this Complex Type:

public class ContactDetails {
    public string Address1 {get;set;}
    public string City {get;set;}
    // ...etc
}

Is used in this Entity:

public class Customer {
    public int ID {get;set;}
    public string Name {get;set;}
    public ContactDetails BillingAddress {get;set;}
    public ContactDetails DeliveryAddress {get;set;}
}

It appears in a database table like this:

CREATE TABLE dbo.Customers (
    ID INT NOT NULL,
    Name NVARCHAR(30),
    BillingAddress_ContactDetails_Address1 NVARCHAR(100),
    BillingAddress_ContactDetails_City NVARCHAR(100),
    DeliveryAddress_ContactDetails_Address1 NVARCHAR(100),
    DeliveryAddress_ContactDetails_City NVARCHAR(100)
)

So I need to change the default naming convention for complex types. I can manually update every column name using Fluent like this:

modelBuilder.Entity<Customer>().Property(x => x.BillingAddress.Address1).HasColumnName("BillingAddress_ContactDetails_Address1");
modelBuilder.Entity<Customer>().Property(x => x.BillingAddress.City).HasColumnName("BillingAddress_ContactDetails_City");
modelBuilder.Entity<Customer>().Property(x => x.DeliveryAddress.Address1).HasColumnName("DeliveryAddress_ContactDetails_Address1");
modelBuilder.Entity<Customer>().Property(x => x.DeliveryAddress.City).HasColumnName("DeliveryAddress_ContactDetails_City");

The problem is that ContactDetails is used many many times throughout the database in many different tables. So I want configure them all in one go. I need something like this:

modelBuilder.ComplexType<ContactDetails>().Property(x => x.Address1).HasColumnName($"{???}_{???}_Address1");
modelBuilder.ComplexType<ContactDetails>().Property(x => x.City).HasColumnName($"{???}_{???}_City");

But how do I fill in the {???} parts? Where can I get property name and type name from in this context?

BG100
  • 4,481
  • 2
  • 37
  • 64

0 Answers0