darta
darta

Reputation: 13

How to Create EF one-to-one relationship with Asp.net Identity

Goal: Create a one-to-one relationship between EF Asp.Net.Identity.User and EF UserBusiness

This is my 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; }

My EF UserBusiness:

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

}

Repository:

==> RepositoryExtension:

public static IQueryable<UserEntity> BuildUserWithBusiness(this       IQueryable<UserEntity> query)
    {
        return query.Include(u => u.Business);
    }

==> Repository

public async Task<UserEntity> GetByIdWithBusinessAsync(string businessId)
    {
        return await _context.Users
            .BuildUserWithBusiness()
            .FirstOrDefaultAsync(x => x.Id == businessId);
    }

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

I got an error on u.User

Error:

'ICollection' does not contain a definition for 'User' and no accessible extension method 'User' accepting a first argument of type 'Collection could be found (are you missing a using directive or an assembly reference?)

Upvotes: 0

Views: 62

Answers (2)

Darshani Jayasekara
Darshani Jayasekara

Reputation: 741

There is a line in your code,

public ICollection<UserBusinessEntity> Business { get; set; }

which should be changed to,

public UserBusinessEntity Business { get; set; }

Also, the model builder should be changed to,

builder.Entity<UserEntity>(entity =>
{
    entity.HasOne(b => b.Business)
        .WithOne(u => u.User);
        .HasForeignKey<BusinessUser>(c => c.Id);
});

Please note that I haven't tried the code real-time.

Upvotes: 2

Related Questions