How to establish a one-to-one relationship with Entity Framework Code First

I read all the Google and SO pages about people who establish a one-to-one relationship in EF, but it just doesn't work for me.

Here is my model:

accounts:

public int Id {get; set;}
public virtual AccountConfig AccountConfig {get; set;}

Account Card:

HasKey(t => t.Id);
HasRequired(t => t.AccountConfig)
    .WithOptional();

Account Configuration:

public int Id {get; set;}
public int AccountId {get; set;}
public virtual Account Account {get; set;}

Account Configuration:

HasKey(t => t.Id);
HasRequired(t => t.Account)
    .WithOptional(t => t.AccountConfig);

When the property AccountConfigon Accountthe same NULL, and the property is Accounton AccountConfiga wrong entry (coincidentally obtained Account.Idcoincides with AccountConfig.Id, but I do not know what that means).

In the database, the table Accountdoes not have a record reference AccountConfig, but the table AccountConfighas a record reference Accountusing a column AccountId.

AccountConfig from Account ( ), Account AccountConfig.

+5
1

EF "--" . AccountConfig Account. EF " " .

1:1 1: 0..1 EF. , EF .

+9

All Articles