0

I have an ASP.NET MVC app using Entity Framework from our SQL Server backend.

Goal is to create ~18 WPackage entries via a foreach loop:

foreach (var dbitem in dbCList)

The code works for a single WPackage entry, but we have a request from the customer to create 300+ WPackages, so trying to use the Entity Framework code for a single "Add" and loop to create 300+ adds.

The T-SQL would be very challenging as there are many keys created on the fly/at row creation, so for activities >> resources, we'd have to insert the activity, grab or remember the activity key, then add resources with that newly created activity key.

Each WPackage (this is the main parent table) could have one or more of the following child table entries:

  • 1+ activities
  • each activity would have 1+ resource
  • 1+ budgets
  • 1+ Signatures
  • 1+ CostCodes

Our schema or model diagram would be:

WPackage
--Activities
-----Resources (child of Activities)
--CostCodes
--Budgets
--Signatures

The following code fails on:

dbContextTransaction.Commit();

with an error:

The transaction operation cannot be performed because there are pending requests working on this transaction.

[HttpPost]
public ActionResult Copy([Bind(Include = "ID,WBSID,...***fields excluded for brevity")] Package model)
{
    if (ModelState.IsValid)
    {
        try
        {
            using (var dbContextTransaction = db.Database.BeginTransaction())
            {
                var dbCList = db.Packages.Join(db.WBS,
   *expression omitted for brevity*)
                // this dbClist will build about 18 items in the collection for below loop

                foreach (var dbitem in dbCList)
                {
                    int testWPID = dbitem;

                    WPackage prvWP = db.WPackages.Find(dbitem);
    
                    int previousWPID = dbitem;

                    WPackage previousWP = db.WPackages.Find(dbitem);
                    model.ID = dbitem;
    
                    db.WPackages.Add(model);
                    db.SaveChanges();
                                
                    var budgets = db.Budgets.Where(i => i.WPID == previousWPID);

                    foreach (Budget budget in budgets)
                    {
                        budget.WPID = model.ID;
                        db.Budgets.Add(budget);
                    }
    
                    var costCodes = db.CostCodes.Where(i => i.WPID == previousWPID);

                    foreach (CostCode costCode in costCodes)
                    {
                        costCode.WPID = model.ID;                                
                        db.CostCodes.Add(costCode);
                    }
    
                    var activities = db.Activities.Where(i => i.WPID == previousWPID);
                    // *code excluded for brevity*
    
                    var previousActivityID = activity.ID;
    
                    db.Activities.Add(activity);
                    db.SaveChanges();
                                    
                    var resources = db.Resources.Where(i => i.ActivityID == previousActivityID);

                    foreach (Resource resource in resources)
                    {
                        resource.WPID = model.ID;                                    
                        resource.ActivityID = activity.ID;
                        resource.ActivityNumber = activity.ActivityNumber;
    
                        db.Resources.Add(resource);
                        db.SaveChanges();
                    }
                }

                var signatures = db.RolesAndSigs
                                   .Where(i => i.KeyId == previousWPID && i.Type == "WPL")
                                   .OrderBy(i => i.Role)
                                   .OrderBy(i => i.Person);

                foreach (RolesAndSig signature in signatures)
                {
                    db.RolesAndSigss.Add(signature);
                }
    
                db.SaveChanges();
                dbContextTransaction.Commit();
            }
        }
    }
}

I've also tried to have the Commit() run outside the foreach dbitem loop like:

     db.SaveChanges();
     //dbContextTransaction.Commit();
 }
 dbContextTransaction.Commit();

...but this returns error of:

[EXCEPTION] The property 'ID' is part of the object's key information and cannot be modified.

1 Answer 1

1

The code you posted has some issues that don't make sense, and probably aren't doing what you think they are doing. The crux of the issue you are facing is that Entity Framework tracks all references to entities it loads and associates:

Firstly this code:

int testWPID = dbitem;
WPackage prvWP = db.WPackages.Find(dbitem);
    
int previousWPID = dbitem;
WPackage previousWP = db.WPackages.Find(dbitem);

prvWP and previousWP will be pointing to the exact same reference, not two copies of the same entity. Be careful when updating either or any other reference retrieved or associated with that same ID. They all point to the same instance. If you do want a stand-alone snaphot reference you can use AsNoTracking().

Next, when you do something like this in a loop:

model.ID = dbitem;
db.WPackages.Add(model);

In the first iteration, "model" is not an entity. It is a deserialized block of data with the Type of the Package entity. As soon as you call .Add(model) that reference will now be pointing to a newly tracked entity reference. In the next loop you are telling EF to change that tracked entity reference's ID to a new value, and that is illegal.

What it looks like you want to do is create a copy of this model for each of the 18 expected iterations. For that what you want to do would be something more like:

foreach (var dbitem in dbCList)
{
    var newModel = new WPackage 
    {
         ID = dbItem,
         WBSID = model.WBSID,
          /// copy across all relevant fields from the passed in model.
    };


    db.WPackages.Add(newModel);


   // ...
}

It would be quite worthwhile to leverage navigation properties for the related entities rather than using explicit joins and trying to scope everything in an explicit transaction with multiple SaveChanges() calls. EF can manage all of the FKs automatically rather than essentially using it as a wrapper for individual ADO CRUD operations.

You will need to be explicit between when you want to "clone" an object reference vs. "copy" a reference. For example, if I have a Customer that has an Address, and Addresses have a Country reference, when I clone a Customer, I will want to clone a new Address record for that Customer, however ensure that the Country reference is copied across. If I have a record for Jack at an 123 Apple Street, London in England, and go to clone Jack to make a record for Jill at the same address, they might be at the same location now, but not always, so I want them to point at different Address records in case Jill moves out. Still, there should only be one record for "England". (Jill may move to a different country, but her address record would just point at a different Country Id)

Wrong:

var jill = context.Customers.Single(c => c.Name == "Jack");
jill.Name = "Jill";
context.Customers.Add(jill);

This would attempt to rename Jack into Jill, then "Add" the already tracked instance, resulting in an exception.

Will work, but still Wrong:

var jack = context.Customers.AsNoTracking().Single(c => c.Name == "Jack");
var jill = jack;
jill.Name = "Jill";
context.Customers.Add(jill);

This would technically work by loading Jack as an untracked entity, and would save Jill as a new record with a new Id. However this is potentially very confusing. Depending on how the AddressId/Address is referenced we could end up with Jack and Jill referencing the same single Address record. Bad if you want Jack and Jill to have different addresses.

Right:

var jack = context.Customers
    .Include(c => c.Address)
        .ThenInclude(a => a.Country)
    .Single(c => c.Name == "Jack");
var jill = new Customer
{
    Name = "Jill",
    // copy other fields...
    Address = new Address
    {
       StreetNumber = jack.Address.StreetNumber,
       StreetName = jack.Address.StreetName,
       Country = jack.Address.Country
    }
};
context.Customers.Add(jill);

The first detail is to ensure when we load Jack that we eager load all of the related details we will want to clone or copy references to. We then create a new instance for Jill, copying the values from Jack, including setting up a new Address record. The Country reference is copied across as there should only be ever a single record for "England".

Edit: For something like a roll-over scenario if you have a package by year, let's use the example of a Package class below:

public class Package
{
    [Key] 
    public int PackageId { get; set; }
    [ForeignKey("PackageType")]
    public int PackageTypeId { get; set; }
    public int Year { get; set; }

    // .. More package related details and relationships...

    public virtual PackageType PackageType { get; set; }
}

A goal might be to make a new Package and related data for Year 2022 from the data from 2021, and apply any changes from a view model passed in.

Find is a poor choice for this because Find wants to locate data by PK. If you're method simply passes an entity to be copied from (I.e. the data from 2021) then this can work, however if you have modified that data from 2021 to represent values you want for 2022 that could be dangerous or misleading within the code. (We don't want to update 2021's data, we want to create a new record set for 2022) To make a new Package for 2022 we just need the updated data to make up that new item, and a way to identify a source for what to use as a template. That identification could be the PK of the row to copy from (ProductId), or derived from the data passed in. (ProductTypeId, and Year-1) In both cases if we want to consider related data with the "copy from" product then it would be prudent to eager load that related data in one query rather than going back to the database repeatedly. Find cannot accommodate that.

For instance if I want to pass data to make a new product I pass a ProductTypeId, and a Year along with any values to use for the new structure. I can attempt to get a copy of the existing year to use as a template via:

var existingProduct = context.Products
    .Include(x => x.Activities) // Eager load related data.
    .Include(x => x.CostCodes)
    // ...
    .Single(x => x.ProductTypeId == productTypeId && x.Year = year - 1);

or if I passed a ProductId: (such as if I could choose to copy the data from a selected year like 2020 instead)

var existingProduct = context.Products
    .Include(x => x.Activities)
    .Include(x => x.CostCodes)
    // ...
    .Single(x => x.ProductId == copyFromProductId);

Both of these examples expect to find one, and only one existing product. If the request comes in with values that it cannot find a row for, there would be an exception which should be handled. This would fetch all of the existing product information that we can copy from, alongside any data that was passed into the method to create a new Product.

Sign up to request clarification or add additional context in comments.

3 Comments

Steve Py, thanks so much for the input. These lines can be ignored, as they were during initial testing: int testWPID = dbitem; WPackage prvWP = db.WPackages.Find(dbitem); I'll take a look at your code this weekend or early next week and report back. Thank you again for the help!
OriginalValues sounds like some kind of code logic in the DbContext itself or exposing the change tracking that there is an assumption that the method in question would be only called for updated entities. Regarding the roll-over type scenario I will add something to the answer above shortly to cover off typical considerations for that.
Looks like using the new model method works. The OriginalValues was due to a not null field value needing to be provided. The exception error isn't overly helpful, you just have to dig into the debugger and go down the hierarchy to finally find what field was causing the issue.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.