Entity-Framework – EF Core 2.0 – Cyclic Dependence, both ends need FK

I have a fairly simple data model with two entities:

public class User
{
public int Id {get; set; }
public string Name {get; set; }
public int CurrentLocationId {get; set; }

public List< Location> Locations {get; set; }
public Location CurrentLocation {get; set; }
}

and

public class Location
{
public int Id {get; set; }
[Required]
public int UserId {get; set; }
public string Address {get; set; }

public User User {get; set; }
}

Then, in order to successfully run the migration, I need the following model builder code:

builder.Entity()
.HasOne(x => x.User)
.WithMany(x => x.Locations)
.HasForeignKey(x => x.UserId);

This produced a database I expected and the way I needed it. However, due to the following circular dependency error, I could not save the entity:

p>

InvalidOperationException: The changes cannot be saved because a circular dependency is detected in the data to be saved:’ForeignKey: User {‘CurrentLocationId’}-> Location {‘Id’} ToPrincipal: CurrentLocati on,ForeignKey: Location {‘UserId’} – >User{‘Id’} ToDependent: Location ToPrincipal: User’.

Is there a solution in EF Core 2.0?

I have some options to surround it by changing my data model, but this is the preferred method because I can use database constraints to ensure that all locations are linked back to the user, and each user must set CurrentLocation. I know it can solve the problem, but I can’t really allow null values ​​on the CurrentLocation field!

The code I used to try and store users is as follows (simplified for demonstration purposes):

var location = new Location
{< br /> Address = "Some address"
};

_context.Locations.Add(location);

var user = new User
{
Name = "Stu"
};

_context.Users.Add(user);

user.Locations = new List< br />{
location
};

user.CurrentLocation = location;

await _context.SaveChangesAsync();

And I tried it too

var location = new Location
{
Address = "Some address"
};

var user = new User
{
Name = "Stu",
Locations = new List
{
location
},
CurrentLocation = location
};

_context.Users.Add(user);
await _context.SaveChangesAsync();

But the error is still the same. Can this be fixed by some kind of fancy model builder override? Or am I restricting changes to my data model/allowing null values ​​I shouldn’t really allow null values?

Thanks in advance!

answer my own question-just checked, SQL server does not support deferrable constraints, so EF can’t do it anyway! Change to the data model.

I have a fairly simple data model that contains two entities:

public class User 
{
public int Id {get; set; }
public string Name {get; set; }
public int CurrentLocationId {get; set; }

public List Locations {get; set; }
public Location CurrentLocation {get; set; }
}

and

public class Location
{
public int Id {get; set; }
[Required]
public int UserId {get; set; }
public string Address {get; set; }

public User User {get; set; }
}

Then, in order to successfully run the migration, I need the following model builder Code:

builder.Entity()
.HasOne(x => x.User)
.WithMany(x => x. Locations)
.HasForeignKey(x => x.UserId);

This produces a database that I expect and the way I need it. However, due to the following circular dependency error, I cannot Save entity:

InvalidOperationException: Unable to save changes because a circular dependency was detected in the data to be saved:’ForeignKey: User {‘CurrentLocationId’} -> Location {‘Id’} ToPrincipal: CurrentLocation, ForeignKey: Lo cation {‘UserId’} – >User{‘Id’} ToDependent: Location ToPrincipal: User’.

Is there a solution in EF Core 2.0?

I have some options to surround it by changing my data model, but this is the preferred method because I can use database constraints to ensure that all locations are linked back to the user, and each user must set CurrentLocation. I know it can solve the problem, but I can’t really allow null values ​​on the CurrentLocation field!

The code I used to try and store users is as follows (simplified for demonstration purposes):

var location = new Location
{< br /> Address = "Some address"
};

_context.Locations.Add(location);

var user = new User
{
Name = "Stu"
};

_context.Users.Add(user);

user.Locations = new List< br />{
location
};

user.CurrentLocation = location;

await _context.SaveChangesAsync();

And I tried it too

var location = new Location
{
Address = "Some address"
};

var user = new User
{
Name = "Stu",
Locations = new List
{
location
},
CurrentLocation = location
};

_context.Users.Add(user);
await _context.SaveChangesAsync();

But the error is still the same. Can this be fixed by some kind of fancy model builder override? Or am I restricting changes to my data model/allowing null values ​​I shouldn’t really allow null values?

Thanks in advance!

Answer my own question-I just checked, SQL server does not support deferrable constraints, so EF can’t do it anyway! Change to data model.

WordPress database error: [Table 'yf99682.wp_s6mz6tyggq_comments' doesn't exist]
SELECT SQL_CALC_FOUND_ROWS wp_s6mz6tyggq_comments.comment_ID FROM wp_s6mz6tyggq_comments WHERE ( comment_approved = '1' ) AND comment_post_ID = 2288 ORDER BY wp_s6mz6tyggq_comments.comment_date_gmt ASC, wp_s6mz6tyggq_comments.comment_ID ASC

Leave a Comment

Your email address will not be published.