damienbod / AspNetCoreLocalization

Localization.SqlLocalizer & ASP.NET Core MVC Localization Examples
http://damienbod.com/2015/10/21/asp-net-5-mvc-6-localization/
MIT License
251 stars 101 forks source link

Problem "The instance of entity type 'LocalizationRecord' cannot be tracked" #45

Closed houssam-saissi closed 7 years ago

houssam-saissi commented 7 years ago

Hello,

Thank you very much for your project. it's very interesting :). However, when I run the project the first time I get the error in the screenshot (screen-1.png).

I checked at the database and the row is inserted properly. When I inspect the code, it is if it passes 2 times by the function "_context.LocalizationRecords.Add (localizationRecord);" In the "DevelopmentSetup.cs"

Note that after a second use, the page is displayed correctly.

This problem unfolds each time a new resource is added to the project.

Any help ? Thank you :)

screen-1

damienbod commented 7 years ago

I'll check this, had to rework the lifecycles in the .NET 2.0, What database do you use?

Greetings Damien

damienbod commented 7 years ago

I think I see the problem, will fix this today, or at the latest tomorrow

houssam-saissi commented 7 years ago

Hi, Thank you for reply :) .

I use a Sqlite database as in the project example. I think we have to create the resource if it does not exist otherwise we just have to read it.

I use .Net Core 2.0

Thanks,

houssam-saissi commented 7 years ago

I've added this code to avoid the error, but I think this has to be done differently :) :

            var existingRecord = _context.LocalizationRecords.Where(data => data.ResourceKey.Equals(resourceKey) && data.Key.Equals(key) && data.LocalizationCulture.Equals(culture)).FirstOrDefault();

            if (existingRecord == null)
            {
                _context.LocalizationRecords.Add(localizationRecord);
            }
            else
            {
                localizationRecord.Id = existingRecord.Id;
                _context.LocalizationRecords.Update(existingRecord);
            }
            _context.SaveChanges();
damienbod commented 7 years ago

fixed, will release this in version 2.0.2

damienbod commented 7 years ago

Hi @houssam-saissi Thanks for reporting.

fixed in https://www.nuget.org/packages/Localization.SqlLocalizer/2.0.2

Could you please verify

reetings Damien

houssam-saissi commented 7 years ago

Hi @damienbod

Thaanks it works very well solution :) Thanks for helping me, your project is so great. I recommend this project.

houssam-saissi commented 7 years ago

Hi @damienbod

Thanks for your help, it works very well :) The problem is solved.