目标:在EF Asp.Net.Identity.User和EF UserBusiness 之间创建一对一关系
这是我的EF Asp.Net.Identity.User:
public class UserEntity:IdentityUser
{
public override string Id { get; set; }
public override string UserName { get; set; }
public override string Email { get; set; }
public override string NormalizedUserName { get; set; }
public override string NormalizedEmail { get; set; }
public string Telephone { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string MiddleName { get; set; }
public string Address { get; set; }
public string City { get; set; }
public string ZipCode { get; set; }
public string Country { get; set; }
public DateTime? DateOfBirth { get; set; }
public string NIF { get; set; }
public string Gender { get; set; }
public string Password { get; set; }
public bool IsUserProfileCompleted { get; set; }
**public ICollection<UserBusinessEntity> Business { get; set; }**
public ICollection<PatientEntity> Patients { get; set; }
public DateTime UpdatedOn { get; set; }
public bool IsDeleted { get; set; }
public DateTime CreatedOn { get; set; }
我的EF用户业务:
[Table ("UserBusiness")]
public class UserBusinessEntity
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public string BusinessId { get; set; }
public string Name { get; set; }
public string Address { get; set; }
public string City { get; set; }
public string ZipCode { get; set; }
public string Country { get; set; }
public string Telephone { get; set; }
public string Email { get; set; }
public string Fax { get; set; }
public string Owner { get; set; }
public string OwnerPointofContact { get; set; }
public DateTime CreatedOn { get; set; }
public DateTime DeletedOn { get; set; }
public DateTime UpdatedOn { get; set; }
[ForeignKey("Id")]
public string Id { get; set; }
public virtual UserEntity User { get; set; }
}
存储库:
===>存储库扩展:
public static IQueryable<UserEntity> BuildUserWithBusiness(this IQueryable<UserEntity> query)
{
return query.Include(u => u.Business);
}
===>存储库
public async Task<UserEntity> GetByIdWithBusinessAsync(string businessId)
{
return await _context.Users
.BuildUserWithBusiness()
.FirstOrDefaultAsync(x => x.Id == businessId);
}
流利的API:
public class DentalClinicDbContext : IdentityDbContext<UserEntity, UserRoleEntity, string>
{
public DbSet<UserBusinessEntity> UserBusiness { get; set; }
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
builder.Entity<UserEntity>(entity =>
{
entity.ToTable("Users");
});
builder.Entity<UserEntity>(entity =>
{
entity.HasOne(b => b.Business)
.WithOne(u => u.User);
});
我在u上遇到了一个错误。用户
错误:
"Collection"不包含"User"的定义,也找不到接受"Collection"类型的第一个参数的可访问扩展方法"User"(是否缺少using指令或程序集引用?(
代码中有一行,
public ICollection<UserBusinessEntity> Business { get; set; }
应更改为,
public UserBusinessEntity Business { get; set; }
此外,模型构建器应更改为
builder.Entity<UserEntity>(entity =>
{
entity.HasOne(b => b.Business)
.WithOne(u => u.User);
.HasForeignKey<BusinessUser>(c => c.Id);
});
请注意,我还没有实时尝试过该代码。
您应该遵循以下示例:https://www.entityframeworktutorial.net/efcore/configure-one-to-one-relationship-using-fluent-api-in-ef-core.aspx