0

I have a FooDbContext inheriting from BarContext.

In BarContext, with Schema Bar, I have an object Bar.

In FooDbContext, with Schema Foo, I have an object Foo, which has an BarFK and Bar Navigation property.

In BarDbContext's OnModelCreation method I've defined Bar:

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
  // define Foo...
    modelBuilder.Entity<Foo>...etch...
}

And In FooDbContext's OnModelCreation method, I've defined Foo and Ignored the "inherited" Bar entity:

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
  // define Foo...
    modelBuilder.Entity<Foo>...etch...
  // Then ignore Bar
    modelBuilder.Ignore(Bar);
}

I've seeded it with a record to check and run Migrations.

It nearly works. In that it doesn't complain.

The BarFK value is filled as per the seeding -- but when I perform the query with .Include(x=>x.Bar) it complains that type Bar is not part of the model.

I'm not 100% sure, but pretty sure it would work if they both had the same Schema.

But it's hugely important to this app that I can keep the schema's distinct if at all possible.

Is it possible? If so....how?!?

Thanks!

  • Schema (as soon as this is not Oracle database) doesn't matter. You can't reference/load entity which is ignored (`Ignore` means "hey, this class is not an entity, don't do anything with it"). Keeping distinct schemas is ok, but separate db contexts containing linked entities mapped to *one and the same* database doesn't sound good to me. – Ivan Stoev Jun 05 '19 at 11:35
  • Thanks Ivan. I'm trying to follow the examples in: https://stackoverflow.com/questions/11197754/entity-framework-one-database-multiple-dbcontexts-is-this-a-bad-idea/12625918#12625918 and https://stackoverflow.com/questions/27969861/entity-framework-sharing-entities-across-different-dbcontexts ...I agree Ignore sounds like it wouldn't work, but those articles I *think* were saying it could work if ignored (it was a bit sketchy). – user11554720 Jun 05 '19 at 11:57

0 Answers0