-
Notifications
You must be signed in to change notification settings - Fork 3.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Entity Framework instance of entity type cannot be tracked #7599
Comments
An update on my code. By changing
to
It solves the problem for InMemoryDatabase but when using SQLServer I get the error:
BTW, not sure if the change in the configuration is the way to go ... It was just something I tried. |
@mdmoura With these two lines: b.HasOne(x => x.Language).WithMany(x => x.LanguagesI18N).HasForeignKey(x => x.LanguageCode).IsRequired(true).OnDelete(DeleteBehavior.Restrict);
b.HasOne(x => x.Language).WithMany(x => x.LanguagesI18N).HasForeignKey(x => x.TranslationCode).IsRequired(true).OnDelete(DeleteBehavior.Restrict); the foreign key for the relationship is first set to be |
@ajcvickers As an example I am posting a few records of Languages and LanguagesI18N tables: Languages LanguagesI18N So Languages holds all the unique Language codes ... LanguagesI18N holds the names translations. So both LanguageCode and TranslationCode are FK in table LanguagesI18N related to LanguageCode in table Languages. I am using similar structure in other tables in this MultiLanguage database. Example: Product ProductI18N The point is that in ProductsI18N the Id and TranslationCode are different and FK to different tables. In LanguagesI18N table LanguageCode and TranslationCode are FK's to same table but filling different purposes. LanguageCode is the language it is translating, TranslationCode is the language of translation. Does it make sense? For me it seems a pretty normal scenario ... I am just saving all Languages and their names in different languages. |
@mdmoura That makes sense now. You will need to specify these as two different relationships, that use different (or no) navigation properties between the entities. This is what you did in your first comment--you made a new relationship for the TranslationCode FK which had no navigation properties. However, it seems that while trying to enforce this constraint SQL Server was unable to find a row in the Languages table with a primary key value of "pt". Are you sure that the Languages table contains this row? |
@ajcvickers, The FK's have two different names, LanguageCode and TranslationCode but after running the migration in fact I get two columns LanguageCode and LanguageCode1. I think this shouldn't happen ... |
@mdmoura Agreed this shouldn't happen. We will discuss this in the team. As a workaround, try using |
@ajcvickers I tried the following as you suggested:
The database is correctly created but when I try to create a few records I got the error:
I then tried to keep only
But I got the following error when creating the records:
Any way to go around this? |
@mdmoura You cannot reuse the same navigation for two relationships, try this:
|
@AndriySvyryd Thank you, that work. But I ended adding to navigation properties in Language to the two FK in LanguageI18N because it is better for navigation and in fact remove all the problems I add so far. |
This is probably already fixed in 2.0.0 |
Confirmed the other key is no longer created in 2.0.0 |
Problem
I am not able to create two LanguageI18N instances associated with one Language instance. The same error occurs using SQLServer or an InMemoryDatabase.
Is this a bug? Am I missing something? Any way to go around this problem?
Exception
Steps to reproduce
Further technical details
EF Core version: 1.1.0
Database Provider: Microsoft.EntityFrameworkCore.SqlServer and Microsoft.EntityFrameworkCore.InMemory
Operating system: Windows 10
IDE: Visual Studio Code
The text was updated successfully, but these errors were encountered: