-
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
Temporary Key issue when inserting Many To Many Relationship and Self Reference #10142
Comments
Notes for triage: I was able to reproduce this; minimal repro code is: class Program
{
public class Hobbie
{
public int Id { get; set; }
public ICollection<FacebookUsersHobbies> FacebookUsers { get; set; }
}
public class FacebookUser : User
{
public ICollection<FacebookUsersHobbies> Hobbies { get; set; }
}
public abstract class User
{
public int Id { get; set; }
[ForeignKey("Parent")]
public int? ParentId { get; set; }
public User Parent { get; set; }
[InverseProperty("Parent")]
public ICollection<User> Children { get; set; } = new HashSet<User>();
}
public class FacebookUsersHobbies
{
public int UserId { get; set; }
public FacebookUser User { get; set; }
public int HobbieId { get; set; }
public Hobbie Hobbie { get; set; }
}
public class MyContext : DbContext
{
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
=> optionsBuilder
.UseSqlServer(
@"Server=(localdb)\mssqllocaldb;Database=Test;ConnectRetryCount=0");
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Hobbie>();
modelBuilder.Ignore<User>();
modelBuilder.Entity<FacebookUser>();
modelBuilder.Entity<FacebookUsersHobbies>()
.HasKey(uh => new { uh.UserId, uh.HobbieId });
}
}
static void Main(string[] args)
{
using (var context = new MyContext())
{
context.Database.EnsureDeleted();
context.Database.EnsureCreated();
var hobby = context.Add(new Hobbie()).Entity;
context.SaveChanges();
var fb = new FacebookUser
{
Hobbies = new List<FacebookUsersHobbies>
{
new FacebookUsersHobbies
{
HobbieId = hobby.Id
}
}
};
context.Add(fb);
context.SaveChanges();
}
}
} There is a very strange construct in this model--namely, the inverse relationship configured on an unmapped base type. If this is commented out: //[ForeignKey("Parent")]
public int? ParentId { get; set; }
//public User Parent { get; set; }
//[InverseProperty("Parent")]
//public ICollection<User> Children { get; set; } = new HashSet<User>(); then the issue no longer repros. The model created in the working case is:
While the model created in the error case has a discriminator annotation remaining:
Marking for re-triage. |
@AndriySvyryd - Would #10842 also removes Discriminator in above case? |
@smitpatel In this case there's no Discriminator property, only a value. #10842 doesn't remove the value annotation, but it shouldn't impact anything if there's no discriminator. Even if the annotation is removed from the model this issue still reproes. |
I have same problem with ef core 2.2 The property 'UserId' on entity type 'UserGroups' has a temporary value. Either set a permanent value explicitly or ensure that the database is configured to generate values for this property. |
@SistemasSouza Please file a new issue with a small, runnable project/solution or complete code listing that demonstrates the behavior you are seeing. |
Configuration
Asp.net core : 2.0.0
EF core : 2.0.0
Npgsql.EntityFrameworkCore.PostgreSQL : 2.0.0
Npgsql.EntityFrameworkCore.PostgreSQL.Design: 2.0.0-preview-1
OS : macOS High Sierra 10.13
Introduction
The first investigation come from this issue : npgsql/efcore.pg#252 (comment)
The code is in a Repository because after multiple exchanges, @ajcvickers was not able to reproduce the issue.
Code
https://github.com/AlexTeixeira/TestEfCoreManyToMany
Step to reproduce
Create the database and run the program from the git repository code.
Error
When the
SaveChanges()
is called, it throw this error :Workaround
Create the user first and Insert the Id instead of the object.
But for me it's extra code for "nothing"
Additionnal informations
I'm not sure, but it seems that when the project is created is macOs the error raise.
I create a more simple project with Self Referencing & Many to Many Relationship on windows and it works.
The text was updated successfully, but these errors were encountered: