[英]Adding a class which uses DbContext to ASP.NET Identity 2 project
[英]How can I merge ASP .NET Identity/ Identity Framework DbContext class to my DbContext class which is on another Assembly/ Project
我已經在我的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類。 請再次記住,它們在同一解決方案下處於不同的組裝中。
如果通過合並意味着要同時為身份和數據使用一個DbContext,則需要執行以下操作:
ApplicationUser
移至DB程序集 EFContext
以繼承IdentityDbContext<ApplicationUser>
(標識程序集也將添加到此程序EFContext
) 這種方法的關鍵是要了解IdentityDbContext只是具有某些默認映射的DbContext。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.