I'm having a problem with a mapping in Entity Framework.

I have the following classes (simplified):

public class Building
{

    public int ID { get; set; }     
    // *.. snip..* other properties
    public Location Location { get; private set; }
}

public class Location
{
    public string Street {get; set;}
    public Country country {get; set}
}
public class Country
{
    public int ID { get; set; } 
    public string Name { get; set; } 
}

Building and Country are entities, they are saved in the database. Location is a value type and should map to the same table as Building.

However, when I map it this way, entity framework wants to map Location to a table as well and is complaining it has no Key. I don't want to give it a key since it belongs to the Building and should not be an entity at all.

I have seen workarounds which say you need to put Country on the Building-class, but that just doesn't feel good (and is semantically just plain wrong).

I'm using Entity Framework 5

share|improve this question
    
You're looking for complex types. – Gert Arnold Jun 24 '13 at 12:14
1  
Yes, I know, the problem is that as soon as that complex type has a reference to an entity, EF wants to put the complex type in a table as well – Kenneth Jun 24 '13 at 13:01
    
What a nice question. Actually, I usually put focus on domain logic, and I don't care at all about how the ORM persists data (therefore I wouldn't see adding an Id column in Location as a big problem). If I really had to care about how data is stored, I'd consider not mixing domain objects with ORM entities, by creating a specific layer to persist (creating entities to attend ORM), though this might put more complexity in the archicteture. If you came to a conclusion, let us know, maybe CQRS solves this. – Alisson Jun 11 '16 at 4:51
    
Yeah, in the end, I just got rid of EF. No ORM is much simpler :-) – Kenneth Jun 13 '16 at 14:30
    
You could use NHibernate instead of the EF. Here you can do that things. – Brian J. Hakim Aug 7 '16 at 16:27

You may mark Location property in Building class with [NotMapped] Attribute.

using System.ComponentModel.DataAnnotations.Schema;
public class Building
{
    [NotMapped]
    public Location Location { get; private set; }
}

Hope that solves your problem!

share|improve this answer
    
I don't think this is what he's looking for. This wouldn't save the locations of entities which is what he wants to do. – Jeroen Vannevel Jun 24 '13 at 12:43
    
Indeed, that doesn't solve it. I want Location inside the Building-table with a reference to the countries-table. I need it mapped – Kenneth Jun 24 '13 at 13:01
    
In that case, I think you will have to map 'location' to a table which relates Building and country tables. – Devasayal Jun 24 '13 at 13:39
    
Hm, I wanted to avoid that. My database structure shouldn't be dependend on my domain model. The location data just belongs to the Building, it shouldn't be in a separate table. – Kenneth Jun 24 '13 at 13:43

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.