EntityFramework-Extensions icon indicating copy to clipboard operation
EntityFramework-Extensions copied to clipboard

i get error when save with _db.bulksavechange();

Open vechet opened this issue 5 years ago • 7 comments

Description

i get error when save with _db.bulksavechange();

var newBrand = new Brand
                    {
                        Name = brand.Name,
                        Memo = brand.Memo,
                        IsSystemValue = false,
                        CreatedBy = DefaultFuntion.GetCurrentUserId(),
                        CreatedDate = DefaultFuntion.GetCurrentDateTime(),
                        Version = 1,
                        StatusId = brand.StatusId
                    };
                    _db.Brands.Add(newBrand);

newBrand.Photo = DefaultFuntion.ResizeImage(150M, 150M, photo);
                    var newPhotoAndVideo = new PhotoAndVideo
                    {
                        FileName = fileName.Split('.').FirstOrDefault(),
                        SortOrder = 0,
                    };
                    newBrand.PhotoAndVideos.Add(newPhotoAndVideo);
                    _db.BulkSaveChanges(operation => operation.BatchTimeout = 3600000);

Exception

If you are seeing an exception, include the full exceptions details (message and stack trace).

Exception message:
The MERGE statement conflicted with the FOREIGN KEY constraint "FK_ItemPhoto_Brand".
The conflict occurred in database "EasyAccounting_523", table "dbo.Brand", column 'Id'.

Stack trace:
```   at DbContextExtensions.(DbContext this, Boolean , Action`1 , Boolean )
   at DbContextExtensions.BulkSaveChanges(DbContext this, Action`1 bulkOperationFactory)
   at SmartInventory.Controllers.BrandController.Create(BrandCustomModel brand) in
   D:\Chuo Vechet\Easy Accounting - Development V5.3.2\Easy Accounting - Development\SmartInventory\Controllers\BrandController.cs:line 133

vechet avatar Oct 08 '20 08:10 vechet

Hello @vechet ,

Is SaveChanges working with this scenario?

If yes, could you provide your Entity Type class and model mapping to allow us to reproduce this scenario? Your case seems pretty simple.

You can send your code here [email protected] if you want to keep them private.

Best Regards,

Jon

JonathanMagnan avatar Oct 08 '20 13:10 JonathanMagnan

Hello @JonathanMagnan yes it work with SaveChanges

i ues entity framework 5 with model that generate from edmx . how can i fix this error ? please help.

vechet avatar Oct 09 '20 01:10 vechet

Could you send a small runnable project here: [email protected]

It will be way faster for us to look at it with a project.

JonathanMagnan avatar Oct 09 '20 14:10 JonathanMagnan

Hello @JonathanMagnan

yes, you can download a small runnable project from google drive url here :

https://drive.google.com/file/d/1uKZPi2-Byr-KqHF4Rl8wtgmE1mLqmhvj/view?usp=sharing

vechet avatar Oct 11 '20 16:10 vechet

Thank you for the project, we can now successfully reproducing it.

Unfortunately, I cannot guarantee yet if we could fix it as we don't support in Entity Framework 5 as much as we do in EF6 or EF Core. For example, the whole identity propagation (which is the error here) has been re-written in our code starting from EF6 but we never bring it back the code in EF5 since it was too much complicated.

We will see if there is still something we can do here.

JonathanMagnan avatar Oct 13 '20 03:10 JonathanMagnan

thanks for your help. can i know which version EF5 that you reproducing to solve above problem.

so, about EF6 have this problem or not?

vechet avatar Oct 13 '20 06:10 vechet

Hello @vechet ,

In EF6, the BulkSaveChanges() doesn't have anymore this issue.

However, if you remove the DefaultEntityFrameworkPropagationValue (https://entityframework-extensions.net/improve-bulk-savechanges), you will likely get it back since when disabled, the same code as EF5 is used.

can i know which version EF5 that you reproducing to solve above problem

I'm not sure to understand this question. There is only one version of EF5 released on NuGet: https://www.nuget.org/packages/EntityFramework/5.0.0

JonathanMagnan avatar Oct 14 '20 14:10 JonathanMagnan