Skip to content
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

1:1 relationship with ID conventional properties synced on both ends #14704

Closed
TomasHubelbauer opened this issue Feb 14, 2019 · 2 comments
Closed
Labels
closed-no-further-action The issue is closed and no further action is planned. customer-reported

Comments

@TomasHubelbauer
Copy link

I am trying to figure out if EF Core can be set so that it keeps ID properties on both ends of a foreign key in sync. I have a model like this:

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

public class Car
{
    public int Id { get; set; }
    public string Make { get; set; }
    public string Model { get; set; }
    public int UserId { get; set; }
    public User User { get; set; }
}

I use the fluent API to tell EF Core which entity is the principal one and which is the dependent one:

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    modelBuilder
        .Entity<User>()
        .HasOne(principal => principal.Car)
        .WithOne(dependant => dependant.User)
        .HasForeignKey<Car>(car => car.UserId);
}

When I do this and then insert:

appDbContext.Users.Add(new User() {
    Name = "John Doe",
    Car = new Car() {
        Make = "Tesla",
        Model = "3",
    },
});

The database ends up in this state (in parentheses are the respective ID property values):

User John Doe (1) has car #1
Car Tesla 3 (1) has user #0

I would like to achieve the dependent ID property to contain the ID of the principal entity after save, so:

User John Doe (1) has car #1
Car Tesla 3 (1) has user #1

I tried to set two FKs, one in each direction, but that didn't help.

Is this possible to achieve using EF Core?

EF Core version: 2.2.2
Database Provider: SQL Server
Operating system: Windows
IDE: CLI

@ajcvickers
Copy link
Contributor

This looks like a many-to-many relationship. (That is, a user can have many cars, and a car can have many users.) If so, check out many-to-many mapping in the docs.

@ajcvickers ajcvickers added the closed-no-further-action The issue is closed and no further action is planned. label Feb 14, 2019
@TomasHubelbauer
Copy link
Author

I don't think this is a many to many relationship, because while there can be multiple cars associated with one user, only one of those cars can be set to CarId on the user entity. So it can be 1:N for user:car and 1:N for car:user but in both cases there are dangling entities in the N. And this is what I am looking to prevent - to have EF ensure for me that this always stays 1:1. Is that possible?

@ajcvickers ajcvickers reopened this Oct 16, 2022
@ajcvickers ajcvickers closed this as not planned Won't fix, can't repro, duplicate, stale Oct 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
closed-no-further-action The issue is closed and no further action is planned. customer-reported
Projects
None yet
Development

No branches or pull requests

2 participants