0

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.

andmattia
  • 357
  • 6
  • 22
  • Your message is a little hard to read, do you think you could try to rephrase some of your sentences and ultimately what your goal is? – Mark C. Jan 30 '18 at 17:40
  • Not possible. Notice that the article ignores `Category` in `ShippingDeptContext`, which does not have a `DbSet`. – aaron Jan 30 '18 at 18:21
  • 1
    If you don't want the migration, you can remove it directly after `Add-Migration`, before `Update-Database`. – aaron Jan 30 '18 at 18:25
  • You can run Update-Database “previous migration name” command to remove the last migration – Vivek Nuna Jan 30 '18 at 18:54
  • Is this useful https://stackoverflow.com/a/20689272/6527049 – Vivek Nuna Jan 30 '18 at 19:12

0 Answers0