Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EF Code First Multiple entities to same table

I am interested in how I can map two entities to same table, by using code first. Here's an example:

public class User
{
    [Key]
    public int UserId { get; set; }
    public string Name { get; set; }
    public byte Age { get; set; }
    public bool Active { get; set; }
    public DateTime Created { get; set; }
}

public class UserViewModel
{
    [Key]
    public int UserId { get; set; }
    public string Name { get; set; }
    public byte Age { get; set; }
}

Basically I'm fed up with building repositories. I want to map all possible models for configuration portal, user portal, other services in modelbuilder and just use DbContext for everything. I want to set User class as top of the hierarchy and a class that builds the database, while all other models should just be there for various applications.

I don't want to use automapper. I've also done fair amount of manual coding which just wasted my time, and every single modification requires me to go back to repository and recode - which annoys me.

I've tried to use this in modelbuilder, but it warns me that hierarchy is not valid:

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<User>().Map(p => { p.ToTable("Users"); });
        modelBuilder.Entity<UserViewModel>().Map(p => { p.ToTable("Users"); });
    }

Also keep in mind that I'm not trying to achieve "Table splitting". I don't want my table to be split in two entities, I want rather to have all columns nullable, except one with primary key, and allow different applications/web services/web portals to populate as much data as they've been granted access for.

Thanks for all the tips :)

like image 262
Admir Tuzović Avatar asked May 03 '12 18:05

Admir Tuzović


People also ask

How do you map multiple entities on the same table?

If you want to map the same database table to two entities, you should create a simple inheritance hierarchy. The superclass should be abstract and contain all attributes that are shared by both entities. You should map it as a mapped superclass so that it is not an entity itself.

How you can load related entities in EF?

Entity Framework supports three ways to load related data - eager loading, lazy loading and explicit loading. The techniques shown in this topic apply equally to models created with Code First and the EF Designer.

Should I use EF core or EF6?

Keep using EF6 if the data access code is stable and not likely to evolve or need new features. Port to EF Core if the data access code is evolving or if the app needs new features only available in EF Core.

Is EF core faster than EF6?

Note that for a single row, EF Core is slower than EF6 unless using context pooling; this could be the cost of setting up all the scoped services for each instance (which isn't done when context pooling is on). For multiple rows, EF Core batches and EF6 doesn't, so EF Core quickly becomes much faster.


2 Answers

You can't. One table = one entity (except advanced mappings like mentioned table splitting and TPH inheritance). View model is not and entity. It is just view on data / projection so handle it that way. You will always work with User and project user to view model you need:

var view = from u in context.Users
           select new UserViewModel
              {
                  UserId = u.UserId,
                  Name = u.Name,
                  Age = u.Age
              };

Make this as reusable method returning IQueryable<UserViewModel> and you can do whatever you want.

like image 138
Ladislav Mrnka Avatar answered Oct 29 '22 19:10

Ladislav Mrnka


Table Per Hierarchy TPH inheritance in entity framework with code first

https://www.youtube.com/watch?v=2i7jahkpeQ8&list=PL6n9fhu94yhUPBSX-E2aJCnCR3-_6zBZx&index=19

like image 34
Subhasis Avatar answered Oct 29 '22 21:10

Subhasis