Reputation: 11
I have two applications: A and B.
A has an entity:
public class Country
{
public string Code;
public string Name;
}
and B has an entity
public class Travel
{
public string Title;
public virtual ICollection<Country> Countries;
}
I can only change application B. I've copied the entity from A to B. This is how the contexts are defined:
ApplicationAContext:
public class ApplicationAContext : DbContext
{
public virtual DbSet<Country> Country;
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Country>().ToTable(nameof(Country), t => t.ExcludeFromMigrations());
}
}
ApplicationBContext:
public class ApplicationBContext : DbContext
{
public virtual DbSet<Travel> travel;
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Country>().ToTable(nameof(Country), t => t.ExcludeFromMigrations());
}
}
What works:
Querying
Migrations
Mapping across contexts (If I load an object of type Travel, I can access the Country with no issues)
What doesn't work:
Travel travel = new Travel();
travel.Countries = _applicationAContext.Country.AsNoTracking().Where(c => c.Code.Contains("A"));
travel.Title = "Foobar";
_applicationBContext.Travel.Add(travel);
_applicationBContext.SaveChanges();
Despite using AsNoTracking(), the application still attempts to update the Country. Outside our development environments this fail as the application user only has read access to the tables of ApplicationA.
I've tried using modelBuilder.Ignore<Country>() in ApplicationBContext, but this removes all relation tables and foreign key contrains in the database.
I understand that the relation exists in both directions, meaning that Country also has a list of Travel entities, but this will never be utilized or accessed.
Additional information:
Applications A and B shares a database, but B only have read access to the tables of A.
Upvotes: 0
Views: 206
Reputation: 11
Solved by using table splitting https://learn.microsoft.com/en-us/ef/core/modeling/table-splitting
Upvotes: 1