EF模式中的多个dbContext

时间:2019-09-15 14:34:20

标签: c# asp.net-mvc repository-pattern dbcontext

EF存储库模式数据库上下文。 我可以在我的Web应用程序上构建一个支持多于一个dbContext的EF存储库模式吗? 因此,第一个数据库上下文用于身份服务器,第二个数据库上下文用于与身份无关的应用程序模型。

编辑。 具有名称空间(firstProjectAttsmpt)的第一个身份模型是默认的。 第二个是自定义的。 amespace FirstProjectAttempt.Models {     //您可以通过向ApplicationUser类添加更多属性来为用户添加个人资料数据,请访问https://go.microsoft.com/fwlink/?LinkID=317594了解更多信息。     公共类ApplicationUser:IdentityUser     {         公共异步任务GenerateUserIdentityAsync(UserManager管理器)         {             //注意,authenticationType必须与CookieAuthenticationOptions.AuthenticationType中定义的匹配             var userIdentity =等待管理员。CreateIdentityAsync(this,DefaultAuthenticationTypes.ApplicationCookie);             //在此处添加自定义用户声明             返回userIdentity;         }     }

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

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

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

    //public DbSet<Supplier> Suppliers { get; set; }

    //public DbSet<Item> Items { get; set; }

    //public DbSet<Customer> Customers { get; set; }
    //public DbSet<Brand> Brands { get; set; }
    //// public DbSet<Cart> Carts { get; set; }
    //public DbSet<Delivery> Deliveries { get; set; }
    //public DbSet<Order> Orders { get; set; }
    //public DbSet<Order_Item> Order_Items { get; set; }
    //public DbSet<Order_Delivery> Order_Deliveries { get; set; }
    //public DbSet<Supplier_Item> Supplier_Items { get; set; }

    public System.Data.Entity.DbSet<Vibeke.Models.Customer> Customers { get; set; }

    public System.Data.Entity.DbSet<Vibeke.Models.technicians> technicians { get; set; }

    public System.Data.Entity.DbSet<Vibeke.Models.Drivers> Drivers { get; set; }

}

}

amespace Vibeke.Models

{     //您可以通过向ApplicationUser类添加更多属性来为用户添加个人资料数据,请访问https://go.microsoft.com/fwlink/?LinkID=317594了解更多信息。     公共类ApplicationUser:IdentityUser     {         公共异步任务GenerateUserIdentityAsync(UserManager管理器)         {             //注意,authenticationType必须与CookieAuthenticationOptions.AuthenticationType中定义的匹配             var userIdentity =等待管理员。CreateIdentityAsync(this,DefaultAuthenticationTypes.ApplicationCookie);             //在此处添加自定义用户声明             返回userIdentity;         }     }

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

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

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        //modelBuilder.Entity<Category>().HasKey(c => c.CategoryId);
        modelBuilder.Configurations.Add(new OrderMap());

        modelBuilder.Configurations.Add(new CategoryMap());
        modelBuilder.Configurations.Add(new SupplierMap());
        modelBuilder.Configurations.Add(new CustomerMap());
        modelBuilder.Configurations.Add(new ItemMap());
      //  modelBuilder.Configurations.Add(new DeliveryMap());
        modelBuilder.Configurations.Add(new OrderItemMap());
        //modelBuilder.Configurations.Add(new StaffMap());
        // modelBuilder.Configurations.Add(new ColorMap());
        //  modelBuilder.Configurations.Add(new SizeMap());

        base.OnModelCreating(modelBuilder);
    }

    //Role Management
    public DbSet<IdentityUserRole> UserInRole { get; set; }
    // public DbSet<ApplicationUser> appUsers { get; set; }
    public DbSet<ApplicationRole> appRoles { get; set; }

    //end


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

    public DbSet<Supplier> Suppliers { get; set; }


    public DbSet<Customer> Customers { get; set; }
    public DbSet<Brand> Brands { get; set; }
    // public DbSet<Cart> Carts { get; set; }
  //  public DbSet<Delivery> Deliveries { get; set; }
    public DbSet<Order> Orders { get; set; }
    public DbSet<Order_Item> Order_Items { get; set; }
  //  public DbSet<Order_Delivery> Order_Deliveries { get; set; }
    public DbSet<Supplier_Item> Supplier_Items { get; set; }
   // public DbSet<Manager> Managers { get; set; }
    public DbSet<Item> Items { get; set; }

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




    public System.Data.Entity.DbSet<Vibeke.Models.technicians> technicians { get; set; }

    public System.Data.Entity.DbSet<Vibeke.Models.Drivers> Drivers { get; set; }

}

}

0 个答案:

没有答案
相关问题