1

I have two EF contexts _inventoryContext and _auctionContext.

_inventoryContext has a property called Items and _auctionContext has one called Auctions. Items is a collection of Item objects which each contain a Guid to identify them uniquely. The Auctions property is a collection of Auction objects which each contain a Guid InventoryReference that refers to one of the elements of Items.

What I want to do is get a list of all inventory items that are not part of an auction. How do I do this?

anatol
  • 1,680
  • 2
  • 24
  • 47
Sachin Kainth
  • 45,256
  • 81
  • 201
  • 304

2 Answers2

2

There is a better solution in EF Core

You can create view as named Auctions one of your context and map the DbSet model in your code. So you can use other context model and table in another context. But you must ensure your db user can access those two contexts. For example in _inventoryContext you can define like that.

public virtual DbSet<Auction> Auctions { get; set; }

modelBuilder.Entity<Auction>(entity =>
{
       entity.ToView("vwAuctions");
}

It's provides you something like that

var result= from x in _inventoryContext.InventoryReference 
        join y in _inventoryContext.Auctions on x.Id equals y.InvRef
        select x;
1

This may be of help to you.

Alternatively, you can do this in 2 steps: First get a collection of GuidReferences from your Auction, and then fetch the Items whose Guid's are included in the collection. There will be a performance hit because of the extra query, and because the framework will need to allocate the Guid collection. But depending on the Item collection size, that may not be a big deal for you.

Another possibility would be to create a view in one database/context that pulls the data from the other. This would be read-only, however.

Community
  • 1
  • 1
PinnyM
  • 35,165
  • 3
  • 73
  • 81