简体   繁体   中英

Get User Details From IdentityDbContext by joining to User Details entity

I have two DB Contexts.

They are IdentityDbContext( referenced by :ApplicationDbContext), and DbEntites (ie. DbContext).

The IdentityDbContext is only for Authentication and UserManager (doesn't contain User details Entity).

The DbEntites is for the rest of the entities except the User (also contains User Details entity).

IdentityDbContext

public class ApplicationUser : IdentityUser
{
   public int? ContactID { get; set; } // User Details Id
}
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
    public ApplicationDbContext()
        : base("DefaultConnection")
    {
        // I want to initialize the Login Entities Automatically So 
        //no Database.SetInitializer<ApplicationDbContext>(null); is used
    }
}

DBEntities

public class DBEntities : DbContext
{
    public DBEntities()
       : base("name=DefaultConnection")
   {
       Database.SetInitializer<DBEntities>(null);
   }
   public DbSet<Contact> Contacts { get; set; } // User Details Entity
}

Now I want to List the UserNames from the ApplicationUser and also their Details like Name, Address, etc which is in DbEntites.

I tried to include the

public class ApplicationUser : IdentityUser
{
   public int? ContactID { get; set; }

   [ForeignKey("ContactID")]
   public Contact Contact { get; set; }
}
public DbSet<Contact> Contacts { get; set; }

to the ApplicationDbContext, but it just gives me the following error whenever i try to get data from ApplicationUser

The model backing the 'ApplicationDbContext' context has changed since the database was created.

I tried using foreign key for the ContactID in the database. But still the same error. How do i solve the Problem?

Please any suggestions or workaround.

Update: Basically I just want to use the Contact Entity in the ApplicationDbContext without the "The model backing the 'ApplicationDbContext' context has changed since the database was created." error and still be able to use it in DbEntites.

I solved the problem by creating a model for the AspNetUsers and AspNetRoles and using in DbEntites . As my DbEntites has SetInitializer to null , it solved my problem.

public class AspNetUsers
{
    [Key]
    public string Id { get; set; }
    public string UserName { get; set; }
    public int? ContactID { get; set; }

    [ForeignKey("ContactID")]
    public Contact Contact { get; set; }
    public ICollection<AspNetRoles> Roles { get; set; } 
}

public class AspNetRoles
{
    [Key]
    public string Id { get; set; }
    public string Name { get; set; }
    public ICollection<AspNetUsers> Users { get; set; } 
}

In DbEntites

modelBuilder.Entity<AspNetUsers>()
                .HasMany(c => c.Roles)
                .WithMany(i => i.Users)
                .Map(t => t.MapLeftKey("UserId").MapRightKey("RoleId").ToTable("AspNetUserRoles"));


public DbSet<AspNetRoles> AspNetRoleses { get; set; }
public DbSet<AspNetUsers> AspNetUserses { get; set; }

This might not be ideal way to solve the problem, but which one is anyways?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM