Jason_Hough
Jason_Hough

Reputation: 464

Need to get the data during an SaveChanges() within EF Core

I need data for a LINQ query that is not already saved to the database. Here is my code:

 foreach (var item in BusProc)
          {
               var WorkEffortTypeXref = new WorkEffortTypeXref
              {
                  SourceDataEntityTypeName = "BusProc",
                   SourceDataEntityTypeId = item.BusProcId,

               };
              _clDBContext.WorkEffortTypeXref.AddRange(WorkEffortTypeXref);


           }

but I need this data in the SQL Database before I do a join LINQ query on the data. Although I don't really want to do a OnSave() after this function because I want to make the whole process transactional.

This is the LINQ I need to execute. What is the best way to do this?

var linqquery = from bupr in BusProc
                           join wrtx in  WorkEffortTypeXref on bupr.BusProcId equals wrtx.SourceDataEntityTypeId
                           // where wrtx.SourceDataEntityTypeName == "BusProc"
                           select new
                           {
                               wrtx.TargetDataEntityTypeId
                           };

Upvotes: 1

Views: 221

Answers (1)

CMGeek
CMGeek

Reputation: 134

First, try to compile your code. This code won't compile and as-is isn't comprehensible. You are treating WorkEffortTypeXref as a class, a singular object and a list all in the first section of code, so we really can't know what it is supposed to be.

Now, as I understand your question, you want to query information that is being added to a table, (currently stored in memory as a collection of some sort) but you want to query it before it is added? What if the table has other rows that match your query? What if the insert violates a constraint and therefore fails? Linq can query an in-memory collection, but you have to choose, are you querying the collection that you have in memory (and isn't yet a row of your table) or the database (which has all of the rules/contstraints/etc that databases provide)? Until the records are saved to your table, they aren't the same thing.

Upvotes: 1

Related Questions