如何将ASP .NET Identity/Identity Framework DbContext类合并到另一个程序集/



我已经在我的MVC应用程序中实现了N层架构(使用代码优先方法(;在这里,我的数据库在单独的程序集中,MVC在单独的程序集中。因此,我希望将我的 asp .net 标识表添加到现有的数据库 dbcontext 类中。

我试图合并它,但它重新创建了我没有asp .net标识表的现有数据库。

下面是 Asp. net identity DbContext Class(它在单独的程序集中(:

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

下面是另一个程序集中现有的数据库数据库上下文类:

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>

那么,如何将 Asp .net Identity DbContext 类合并到我的 DbContext 类中。请再次记住,它们在同一解决方案下处于不同的程序集中。

如果通过合并,你的意思是同时为身份和数据提供一个 DbContext,你需要做的是:

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

这种方法的关键是要理解 IdentityDbContext 只是一个具有一些默认映射的 DbContext。

最新更新