-
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
Unexpected behaviour of AsNoTracking() #19276
Comments
@VitaliiIsaenko I suspect the model configuration doesn't match what you have in the database. This is the model EF is building:
I suspect that maybe you want to use You might want to take a look at the documentation on modeling relationships: https://docs.microsoft.com/en-us/ef/core/modeling/relationships?tabs=fluent-api |
@ajcvickers thank you for helping out! Unfortunately, you had a look at the |
@VitaliiIsaenko This line: .HasPrincipalKey(x => x.CountryId); sets up an alternate key using |
Interesting, thank you! However, it works somehow.. maybe you have a better idea for such relation then? |
@VitaliiIsaenko The reason you are only seeing inconsistent results is that there are multiple entities with the same key. One of the entities is being discarded because it has the same key as an entity that was already retrieved, indicating that these are duplicates. |
So does it mean that |
@VitaliiIsaenko Note that all EF behavior is undefined when the data does not comply with the constraints inherent in the configured model, which is the case here. |
Thank you, I understand. Is there any example of such a relation? I haven't found it anywhere and on SO people suggested only this option. Basically, it's country having the same id for several rows due to locales referencing states having the same id for several rows for the same reason. The state has one country logically but many with the same id because of locales and vice versa. I would appreciate an example or maybe an it is not possible response :) Could not get this relation from documentation only. |
@VitaliiIsaenko Consider this data from your post:
This means that public class AddressDto
{
public int Id { get; set; }
public int CountryId { get; set; }
public CountryLocaleDto CountryLocale { get; set; }
}
public class CountryLocaleDto
{
public int Id { get; set; }
public string Locale { get; set; }
public List<AddressDto> Address { get; set; }
} modelBuilder.Entity<AddressDto>()
.HasOne(e => e.CountryLocale)
.WithMany(e => e.Address)
.HasForeignKey(e => e.CountryId); But this limits each Address to a single CountryLocale. If you also want each address to be associated with many CountryLocale's, then this is a many-to-many relationship that requires a join table. |
@ajcvickers, thank you a lot for your help! Also, thank you, @smitpatel, the link is very useful and explains much. |
AsNoTracking method does not allow to include data for all elements in a collection. Reference properties are filled only for first matching by id objects. Full info can be found here, in my SO question
Steps to reproduce
There is a sample project branch
states-countries-relation
Further technical details
EF Core version: 2.2
Database provider: Pomelo.EntityFrameworkCore.MySql
Target framework: .NET Core 2.2
Operating system: macOS Mojave
IDE: Rider 2019.2
The text was updated successfully, but these errors were encountered: