We want to use one to one optional relationship using Entity Framework Code First. We have two entities.
public class PIIUser
{
public int Id { get; set; }
public int? LoyaltyUserDetailId { get; set; }
public LoyaltyUserDetail LoyaltyUserDetail { get; set; }
}
public class LoyaltyUserDetail
{
public int Id { get; set; }
public double? AvailablePoints { get; set; }
public int PIIUserId { get; set; }
public PIIUser PIIUser { get; set; }
}
PIIUser
may have a LoyaltyUserDetail
but LoyaltyUserDetail
must have a PIIUser
.
We tried these fluent approach techniques.
modelBuilder.Entity<PIIUser>()
.HasOptional(t => t.LoyaltyUserDetail)
.WithOptionalPrincipal(t => t.PIIUser)
.WillCascadeOnDelete(true);
This approach didn't create LoyaltyUserDetailId
foreign key in PIIUsers
table.
After that we tried the following code.
modelBuilder.Entity<LoyaltyUserDetail>()
.HasRequired(t => t.PIIUser)
.WithRequiredDependent(t => t.LoyaltyUserDetail);
But this time EF didn't create any foreign keys in these 2 tables.
Do you have any ideas for this issue? How can we create one to one optional relationship using entity framework fluent api?