0

These are my model classes

enter image description hereI have three tables enter image description here

But when I update my model, the PurchaseReturnDetail table is not there in the model, and it creates a one-to-many relationship between PurchaseReturn and PurchaseDetail.

I'm using repository pattern and I don't know how to insert transactions in PurchaseReturnDetail table.

Following is my code to insert transaction. but it gives exception

Message = "An entity object cannot be referenced by multiple instances of IEntityChangeTracker."

 public async Task<Response> SaveAsync(PurchaseReturn purchaseReturn, List<string> serialNoList)
    {
        PurchaseReturnRepository rep = new PurchaseReturnRepository();

        decimal totalRate = 0;
        foreach (var serialNo in serialNoList)
        {
            var purchaseDetail = await PurchaseService.Instance.GetSinglePurchasedItemDetailAsync(serialNo);

            purchaseReturn.PurchaseDetails.Add(purchaseDetail);

            //Calculating total rate to update supplier balance
            totalRate += purchaseDetail.Rate;


        }

        var response = await rep.AddAsync(purchaseReturn);

        if (response.Success)
        {
            //Updating Supplier Balance
            response = await SupplierService.Instance.UpdataBalanceAsync(purchaseReturn.SupplierId, totalRate, false);
            response.Msg = "Items has beed returned and stock is updated";
        }

        return response;
    }

NOTE : I'm using database first approach

Rizwan Ijaz
  • 111
  • 2
  • 12
  • Show your model classes please? Give the screenshot database table's column list. – TanvirArjel Jan 27 '19 at 07:34
  • Did you intentionally undo the seemingly helpful formatting changes by another user? Be careful with that. What are your reasons for that? Formatting which is perceived as "noisy" is not to your benefit. If it was unintenionally I will be happy to help with some explanation or hints. – Yunnosch Jan 27 '19 at 07:47
  • #Yunnosch yes please – Rizwan Ijaz Jan 27 '19 at 07:54
  • The two FKs should both form a composite PK. Then EF will model this as many-many with a hidden junction class. – Gert Arnold Jan 27 '19 at 09:41
  • Please don't add new questions. What's the use anyway of writing code when the model isn't right? I'd rather see you respond to my previous comment. – Gert Arnold Jan 27 '19 at 13:17
  • #GertArnold what do you suggest me to do? I know Entity Framework does not generate model for table which only have foreign keys. But my requirement is obvious, I don't want any thing else in relational table except foreign keys – Rizwan Ijaz Jan 27 '19 at 13:46
  • Did you understand what I said about the primary key? Both FK fields in `PurchaseReturnDetail` should be one composite primary key. Fix that first and then regenerate the model. Also, if you know that EF doesn't generate a class for tables with foreign keys only then why do you phrase your question as though you seem surprised it's not there? – Gert Arnold Jan 27 '19 at 18:17
  • Both foreign keys are now composite key in `PurchaseReturnDetail` and regenerated the model. Now model is showing many-many relation between `PurchaseDetail` and `PurchaseReturn`. But when I run insert query it gives my exception `Message = "An entity object cannot be referenced by multiple instances of IEntityChangeTracker."` – Rizwan Ijaz Jan 28 '19 at 07:25
  • And that's a different problem, not related to the shape of the model. You have a static context, which is against all recommendations. – Gert Arnold Jan 29 '19 at 08:21
  • Possible duplicate of [entity object cannot be referenced by multiple instances of IEntityChangeTracker. while adding related objects to entity in Entity Framework 4.1](https://stackoverflow.com/questions/10191734/entity-object-cannot-be-referenced-by-multiple-instances-of-ientitychangetracker) – Gert Arnold Jan 31 '19 at 15:51

0 Answers0