问题
I am using EF Core 2.1 for my DAL. This is how my model looks like
One user can have only one role:
// Role entity - kind of master
public class Role
{
public int RoleId { get; set; }
public string RoleName { get; set; }
}
// User entity - every user will have one role
public class User
{
public int UserId { get; set; }
public string Email { get; set; }
public string Password { get; set; }
public int RoleId { get; set; }
public bool IsActive { get; set; }
public DateTime CreatedOn { get; set; }
public virtual Role Role { get; set; }
}
This is how I was thinking/tried to define the relationship between these two entities:
public void Configure(EntityTypeBuilder<User> builder)
{
builder.HasKey(x => x.UserId);
builder.Property(x => x.RoleId).IsRequired(true);
builder.Property(x => x.CreatedOn).IsRequired(true).HasDefaultValue(DateTime.Now);
// FK - throwing compile time error
builder.HasOne(x => x.Role).WithOne(y => y.RoleId);
}
How do I define this one user one role relation using EF Fluent API?
Thanks.
Attachment:-
回答1:
Not only in the FK relationship configuration, you also have problem in the default value generation configuration for the CreatedOn
column. So write your Entity Configuration as follows:
public void Configure(EntityTypeBuilder<User> builder)
{
builder.HasKey(u => u.UserId);
builder.Property(u => u.CreatedOn).IsRequired(true).HasDefaultValueSql("getdate()");
// FK - configuraiton
builder.HasOne(u => u.Role).WithMany().HasForeignKey(u => u.RoleId).IsRequired(true);
}
回答2:
One of the tenets Entity framework is built on is "Convention over configuration" and by convention, I don't think builder.HasOne(x => x.Role).WithOne(y => y.RoleId);
is necessary as by convention you already stated that your User entity should have just one role when you added public int RoleId { get; set; }
and public virtual Role Role { get; set; }
properties.
Remove that and I am certain your application will yield just that behaviour as stated in my comment above.
回答3:
I think you can add Users to Role like this
public class Role
{
public int RoleId { get; set; }
public string RoleName { get; set; }
public List<User> Users {get;set;}
}
Then you will register like this
public void Configure(EntityTypeBuilder<User> builder)
{
builder.HasKey(x => x.UserId);
builder.Property(x => x.RoleId).IsRequired(true);
builder.Property(x => x.CreatedOn).IsRequired(true).HasDefaultValue(DateTime.Now);
builder.HasOne(x => x.Role).WithMany(y => y.Users);
}
来源:https://stackoverflow.com/questions/53681739/how-to-define-foreign-key-relationship-in-ef-core-2-1