I'm trying to use the foreign key association approach to achieve a one-to-one association in EF. In my case there's an association between a User and a Team, and I need a navigation property in each of them. I bumped into a problem when trying to save data.
This is what the models look like:
public class Team
{
public int ID { get; set; }
public string Name { get; set; }
public int OwnerID { get; set; }
public virtual User Owner { get; set; }
}
public class User
{
public int ID { get; set; }
public string UserName { get; set; }
public int TeamID { get; set; }
public virtual Team Team { get; set; }
}
I added these bits to the DbContext
OnModelCreating()
, as instructed in the blog post referenced above:
modelBuilder.Entity<User>()
.HasRequired(u => u.Team)
.WithMany()
.HasForeignKey(u => u.TeamID);
modelBuilder.Entity<Team>()
.HasRequired(t => t.Owner)
.WithMany()
.HasForeignKey(t => t.OwnerID)
.WillCascadeOnDelete(false);
And now when adding data like this:
User u = new User();
u.UserName = "farinha";
Team t = new Team("Flour Power");
u.Team = t;
t.Owner = u;
context.Users.Add(u);
context.Teams.Add(t);
context.SaveChanges();
or even like this:
User u = new User();
u.UserName = "farinha";
u.Team = new Team("Flour Power");
context.Users.Add(u);
context.SaveChanges();
I'm getting the following error:
Unable to determine a valid ordering for dependent operations. Dependencies may exist due to foreign key constraints, model requirements, or store-generated values.
Any idea of how to solve this? Am I saving the data in a wrong way?
Thanks in advance
We can configure a one-to-One relationship between entities using Fluent API where both ends are required, meaning that the Student entity object must include the StudentAddress entity object and the StudentAddress entity must include the Student entity object in order to save it.
Insert DataThe DbSet. Add and DbContext. Add methods add a new entity to a context (instance of DbContext) which will insert a new record in the database when you call the SaveChanges() method. In the above example, context.
SaveChanges() This method will automatically call DetectChanges() to discover any changes to entity instances before saving to the underlying database. This can be disabled via AutoDetectChangesEnabled. Entity Framework Core does not support multiple parallel operations being run on the same DbContext instance.
You are not saving data wrong way but it simply cannot work because you defined bidirectional dependency. Team can be saved only if related to already saved user and user can be saved only if related to existing team. You must make one relation optional by marking foreign key property nullable (for example TeamId
in User
):
public class User
{
public int ID { get; set; }
public string UserName { get; set; }
public int? TeamID { get; set; }
public virtual Team Team { get; set; }
}
Then you must save the User
entity first and then you can save Team
.
User u = new User();
u.UserName = "farinha";
context.Users.Add(u);
context.SaveChanges();
u.Team = new Team { Name = "Flour Power", OwnerID = u.ID };
context.SaveChanges();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With