0

I have a class customer that contains address properties, phone properties and fax properties, but I want to take off the address, phone properties to complex types. Does properties are already in the database as columns.

   [Table("tblCustomer")]
public partial class Customer : Entity 
{
    [Key]
    public int CustomerID { get; set; }

    [StringLength(10)]
    public string CustomerCode { get; set; }

    [StringLength(60)]
    public string AddressLine1 { get; set; }

    [StringLength(70)]
    public string AddressLine2 { get; set; }

    [StringLength(35)]
    public string City { get; set; }

    [StringLength(2)]
    public string State { get; set; }

    [StringLength(10)]
    public string ZipCode { get; set; }

    [StringLength(15)]
    public string PhoneNo { get; set; }

    [StringLength(3)]
    public string PCountryCode { get; set; }

    [StringLength(3)]
    public string PAreaCode { get; set; }

    [StringLength(7)]
    public string PPhoneNo { get; set; }

    [StringLength(3)]
    public string FCountryCode { get; set; }

    [StringLength(3)]
    public string FAreaCode { get; set; }

    [StringLength(7)]
    public string FaxNumber { get; set; }

    [StringLength(3)]
    public string CountryCode { get; set; }
}

how to refactor this into:

[Table("tblCustomer")]
public partial class Customer : Entity 
{
    [Key]
    public int CustomerID { get; set; }

    [StringLength(10)]
    public string CustomerCode { get; set; }

    public Address Address { get; set; }

    public Phone Phone { get; set; }

    public Phone Fax { get; set; }

}

without conflicting with what already exist in the database?

ddieppa
  • 5,866
  • 7
  • 30
  • 40

1 Answers1

0

Your address class should be annotated with the ComplexType attribute and you'd need to explicitly map the column names:

[ComplexType]
public class Address
{
    [Column("street")]
    public string Street {get; set;}

    // Other properties
}
Kenneth
  • 28,294
  • 6
  • 61
  • 84
  • Thanks for the answer, but that case will work if I only have one property of type Address, but in case I have HomeAddress and WorkAddress won't work @Keneth `System.Data.Entity.ModelConfiguration.ModelValidationException: One or more validation errors were detected during model generation: AddressLine1: Name: Each property name in a type must be unique. Property name 'AddressLine1' is already defined.` – ddieppa Mar 10 '14 at 13:03
  • 1
    Then you need to make sure that the column names in the database match the pattern `nameofcustomerproperty_nameofaddressproperty` and leave the properties in address without the annotations. EF will then figure it out by itself. Unfortunately, you can't define custom column names if you have more than 1 property of the same type on your customer class. – Kenneth Mar 10 '14 at 13:37