EF 4.2, CodeFirst - navigation property in add-on class

I know that I can solve this by clicking everything on a single entity, and not imposing complex types (since they are only 1-1 mappings), but I like the way of grouping properties in the generated OM.

I have a Customer object that contains the add-on type "CrmData". The CrmData object has a complex address type.

public class Customer { [Required] public CrmSpecificData CrmData { get; set; } } [ComplexType] public class CrmSpecificData { [MaxLength(40)] public string FirstName { get; set; } [MaxLength(80)] public string LastName { get; set; } public Address Address { get; set; } } [ComplexType] public class Address { [MaxLength(150)] public string Address1 { get; set; } [MaxLength(150)] public string Address2 { get; set; } [MaxLength(100)] public string City { get; set; } [MaxLength(15)] public string PostalCode { get; set; } public StateProvince StateOrProvince { get; set; } public virtual CountryRegion CountryOrRegion { get; set; } } 

The StateProvince and CountryRegion types are objects in my database (similar to how DB DB AdventureWorks works). The problem is that when EF tries to create a model, it fails:

The type "MyCo.Crm.Entities.StateProvince" is already configured as an object type. It cannot be reconfigured as a complex type.

I tried to make StateProvince a complex type, but this does not solve the problem. Ideas?

 public class StateProvince { [Key] public int StateProvinceId { get; set; } [MaxLength(3)] public string StateProvinceCode { get; set; } [MaxLength(50)] public string Name { get; set; } } 
+7
source share
1 answer

Complex types cannot contain navigation properties . Navigation properties can only be defined in the entity. Therefore, you must:

  • Use table splitting instead of complex types, but this will lead to other problems - for example, you cannot nest these types, and you will have to use impatient / lazy loading to load them.
  • Move all navigation properties to the main object
+14
source

All Articles