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 :)
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.
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.
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.
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.
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.
Table Per Hierarchy TPH inheritance in entity framework with code first
https://www.youtube.com/watch?v=2i7jahkpeQ8&list=PL6n9fhu94yhUPBSX-E2aJCnCR3-_6zBZx&index=19
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With