如何将ASP .NET Identity / Identity Framework DbContext类合并到另一个Assembly /项目上的DbContext类

时间:2019-09-06 17:45:17

标签: c# asp.net-mvc entity-framework model-view-controller asp.net-identity

我已经在我的MVC应用程序中实现了N层体系结构(使用代码优先方法);在这种情况下,我的数据库分别位于单独的程序集中,而MVC位于单独的程序集中。因此,我希望将asp .net身份表添加到现有数据库dbcontext类中。

我尝试将其合并,但是它重新创建了我现有的数据库,没有asp .net身份表。

这里是Asp。网络身份DbContext类(在单独的程序集中:

namespace MVC_ExplorePak.Models
{

public class ApplicationUser : IdentityUser
{
    public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
    {
        // Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
        var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
        // Add custom user claims here
        return userIdentity;
    }
}

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
    public ApplicationDbContext()
        : base("DefaultConnection", throwIfV1Schema: false)
    {
    }

    public static ApplicationDbContext Create()
    {
        return new ApplicationDbContext();
    }
}

以下是我在另一个程序集中的现有数据库dbcontext类:

namespace EF_DB
{
public class EFContext: DbContext
{
    public EFContext(): base("Constr")
    {

    }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);

        modelBuilder.Entity<Product>()
        .HasMany<Color>(p => p.ColorsOffered)
        .WithMany();

        modelBuilder.Entity<Product>()
            .HasMany<Size>(p => p.SizesOffered)
            .WithMany();

        modelBuilder.Entity<PackageInfo>()
            .HasMany<TourGuide>(p => p.TourGuide)
            .WithMany();

        modelBuilder.Entity<PackageInfo>()
        .HasMany<TourDay>(p => p.TourDays)
        .WithMany();

        //Necessity of time
        modelBuilder.Entity<Hotel>()
        .HasMany<HotelRoom>(p => p.RoomsInformation)
        .WithMany();
    }

    public DbSet<Country> Countries { get; set; }

    public DbSet<Province> Provinces { get; set; }

    public DbSet<City> Cities { get; set; }

    public DbSet<Location> Locations { get; set; }

    public DbSet<Department> Departments { get; set; }

    public DbSet<Category> Categories { get; set; }

    public DbSet<SubCategory> SubCategories { get; set; }

    public DbSet<Size> Sizes { get; set; }

    public DbSet<Color> Colors { get; set; }

    public DbSet<Fabric> Fabrics { get; set; }

    public DbSet<Product> Products { get; set; }

    public DbSet<Hotel> Hotels { get; set; }

    public DbSet<HotelRoom> HotelRooms { get; set; }

    public DbSet<PackageInfo> PackageInfos { get; set; }

    public DbSet<TourGuide> TourGuides { get; set; }

    public DbSet<DriverDetail> DriverDetails { get; set; }

    public DbSet<VehicleInfo> VehicleInfos { get; set; }

}
}

最后,这是我在MVC程序集中的连接字符串:

  <connectionStrings>
    <clear />
    <add name="Constr" connectionString="Data Source=.;Initial Catalog=ExplorePakDB;user id=myid;password=mypass" providerName="System.Data.SqlClient" />
  </connectionStrings>

因此,如何在我的DbContext类中合并Asp .net Identity DbContext类。请再次记住,它们在同一解决方案下处于不同的组装中。

1 个答案:

答案 0 :(得分:0)

如果通过合并您的意思是为身份和数据同时拥有一个DbContext,那么您需要做的是:

  1. ApplicationUser移至数据库程序集
  2. 更改EFContext以继承IdentityDbContext<ApplicationUser>(标识程序集也将添加到该程序集中)
  3. 添加身份表的映射,您可以在这里找到它们:https://github.com/aspnet/AspNetIdentity/blob/master/src/Microsoft.AspNet.Identity.EntityFramework/IdentityDbContext.cs

此方法的关键是要了解IdentityDbContext只是具有某些默认映射的DbContext。