I want to try to share a class (es. user) from two dbcontext.
The idea is 1st dbcontext has all method to work with user (add/remove/update/etc) the 2nd dbcontext only need to access (read the user data).
To do that I have created a class abstract Users and I add this class to the db context, so the problem is when create the migration for the 1st dbcontext and the 2nd.
In my 1st I want to track the changes on my POCO user class in the second I don't want to track/create the table.
I try to use Ingore on OnModelCreating but in this way the code first not create the migration but dbcontext don't have the correct model creating and I'm not able to do operation to table db (i use this approch suggested on EF Data Pints
--Update--
[Table("User")]
public abstract class BaseUser {
[Key]
public virtual int Id {get;set;}
public virtual string Name {get;set;}
}
//DB context 1
public class OUser : BaseUser{
}
public class oneDbContext : DbContext
{
public virtual IDbSet<OUser> OneUsers { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
....
}
//DB context 2
public class MUser : BaseUser{
}
public class twoDbContext : DbContext
{
public virtual IDbSet<MUser> TwoUsers { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Ignore<MUser>();
}
So now if I call the code first with Add-Migration whith modelBuider.Ingore the table User not appear in the script but when I try to access to table via db context I catch the error
The entity type Muser is not part of the model for the current context.
But if I uncomment the ingore the access to table work fine but model try to add the table User.