voam
voam

Reputation: 1016

EF mapping question

Lets say I have the following classes:

public class User
{
    public int UserId { get;set;}
    public string Firstname { get;set;}
    public Interests Interests { get;set;}
}
public class Interests
{
    public IList<Team> FavoriteTeams { get;set;}
    public IList<Food> FavoriteFoods { get;set;}
}
public class Team{
    public int TeamId { get;set;}
    public string City {get;set;}
    public string Name { get;;set;}
}
public class Food{
    public int FoodId { get;set;}
    public string FoodName { get;set;}
}

At the database level I would like to have the following tables:

Users Teams Foods UserTeams UserFoods

Basically I would like to have the Interests property of the user entity but not have a seperate table for it.

I am using EF 4.1 code first POCO classes. I know the easiest thing to do is put the Teams and Food collection as a property on the User, which is ok, but from the object model point of view I would like it be be its own property. Any idea of how I would acheive this type of mapping?

Upvotes: 1

Views: 105

Answers (1)

Slauma
Slauma

Reputation: 177133

I would say: The mapping you are looking for is not possible, because:

If you don't want the class Interests to be mapped to a table, it must not be an entity and the Interests property in your User class must not be a navigation property. To avoid that EF interprets the Interests property as a navigation property, you would only have two options:

  • Mark it as NotMapped -> Does not help because you would lose the relationship from User to Team and Food completely
  • Mark the Interests class as ComplexType -> Not possible in your example because complex types are not allowed to contain navigation properties.

That's just from my level of understanding. It's not unlikely that I overlooked another option.

Upvotes: 3

Related Questions