0

I have the following class:

public class CompanyInfo
    {
        [Key]
        public string CompanyId { get; set; }
        public string CompanyName { get; set; }
        public Address CompanyAddress  { get; set; }
    }

With the corresponding Address class:

public class Address
    {
        public string StreetAddress { get; set; }
        public string StreetAddress2 { get; set; }
        public string City { get; set; }
        public string State { get; set; }
        public string Zip { get; set; }
        public string Plus4 { get; set; }
    } 

Here lies my problem ... the address now generates column names of type:

CompanyAddress_StreetAddress

where I would prefer something along the line of:

PrimaryStreetAddress

Given that the members of the Address class are not readily accessible for use with Data Annotation (using Domain Model/ViewModel structure) and are mapped in the controller as such:

var company = new CompanyInfo
            {
                CompanyId = GenerateAccountNumber(),
                CompanyName = addCompany.CompanyName,
                CompanyAddress = new Address
                {
                    StreetAddress = addCompany.StreetAddress,
                    StreetAddress2 = addCompany.StreetAddress2,
                    City = addCompany.City,
                    State = addCompany.State,
                    Zip = addCompany.Zip,
                    Plus4 = addCompany.Plus4
                },

                  -- some other fields here --

            };

I know that the ViewModel is not the proper venue as it is just being mapped in the controller back to the model.

What would be the proper way to set the column names for the reusable class 'Address'? I'm assuming that this could be done through FluentAPI if Entity Framework but I am uncertain in how to proceed.

Any help would be greatly appreciated.

New2ASPMVC
  • 102
  • 1
  • 11
  • possible duplicate of [Change Table and Column Name Mappings Entity Framework v4.3](http://stackoverflow.com/questions/10554888/change-table-and-column-name-mappings-entity-framework-v4-3) – mason Mar 13 '15 at 13:17
  • Not a duplicate. Look closer at the unique use case. I saw this prior to submitting and it doesn't match my use case. – New2ASPMVC Mar 13 '15 at 13:29
  • @mason this is not duplicate of that question as OP wants to name the column which is not present in his model or generated by EF – Jenish Rabadiya Mar 13 '15 at 13:30
  • You can use the data annotations if you modify your domain model so that the addresses end up in their own table and you refer to the address from your CompanyInfo class via a foreign key. – mason Mar 13 '15 at 13:37

2 Answers2

0

Use ForeignKey data annotation so that ef does not create its own foreign key with its own convention.

public class CompanyInfo
{
    [Key]
    public string CompanyId { get; set; }
    public string CompanyName { get; set; }
    public Address CompanyAddress  { get; set; }
    [ForeignKey("CompanyAddress")]
    public string PrimaryStreetAddress  { get; set; }
}
Jenish Rabadiya
  • 6,708
  • 6
  • 33
  • 62
0

You can override in your context method OnModelCreating to specify the relation between CompanyInfo and Address, and you can specify the foreign key name that will identify the relation.

public class DataContext : DbContext {

    //other properties

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
         modelBuilder.Entity<CompanyInfo>()
            .HasRequired(m => m.CompanyAddress)
            .WithOptional()
            .Map(m => { m.MapKey("CompanyAddressId"); });
    }
}
adricadar
  • 9,971
  • 5
  • 33
  • 46
  • I was trying to avoid the use of a foreign key and just reuse the Address class dynamically for all address use cases within the application, but this is definitely a viable solution. Thanks! – New2ASPMVC Mar 13 '15 at 13:49