【问题标题】:How to Create EF one-to-one relationship with Asp.net Identity如何使用 Asp.net Identity 创建 EF 一对一关系
【发布时间】:2021-09-09 18:38:51
【问题描述】:

目标:在 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.User 上遇到错误

错误:

“ICollection”不包含“用户”的定义,并且没有可访问的扩展方法“用户”接受“集合”类型的第一个参数(您是否缺少 using 指令或程序集引用?)

【问题讨论】:

    标签: asp.net entity-framework linq


    【解决方案1】:

    您的代码中有一行,

    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);
    });
    

    请注意,我没有实时尝试过代码。

    【讨论】:

    • @urmat abdykerimov 我已经进行了上述更改。一切正常。我只能在 ICollection 是一对多关系时使用它。谢谢。
    • @dartarubens,如果这解决了您的问题,请您标记答案。
    【解决方案2】:
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-12
    • 2018-05-27
    相关资源
    最近更新 更多