简体   繁体   English

在多个项目中共享数据库,采用代码优先方法

[英]sharing database in multiple projects, codefirst approach

I have two project inside vs solution 我在vs解决方案中有两个项目

Product.Domain and Product.Web.UI Product.DomainProduct.Web.UI

On both project I want to use code first approach and be able to work with data (domain and ui project). 在两个项目中,我都希望使用代码优先方法,并且能够使用数据(域和ui项目)。

So I created inside domain project simple class which implements DbContext I have two projects, Product.Domain and Product.UI 所以我创建了实现DbContext的域项目内部简单类,我有两个项目Product.Domain和Product.UI

I'm using entity framework code first approach. 我正在使用实体框架代码优先方法。 Inside MasterProject I have class which represents db table Products 在MasterProject内部,我有代表db表产品的类

public class ProductsDbContext : DbContext{
      public ProductsDbContext() : base("name=ProductsDbContext") { }
 }

in my app.config I have connection string ProductsDbContext which handled connection to db. 在我的app.config中,我具有用于处理与db的连接的连接字符串ProductsDbContext。

Now I want to use ApplicationDbContext inside UI project which should use same database from domain layer and store identity tables (which initially does not exist in database when domain layer was created db) I tried to set ProductsDbContext in web.config connection string and to use in ApplicationDbContext constructor like 现在,我想在UI项目中使用ApplicationDbContext,该项目应该使用域层中的相同数据库并存储标识表(当创建域层db时数据库中最初不存在该表)我试图在web.config连接字符串中设置ProductsDbContext并使用在ApplicationDbContext构造函数中

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
    public ApplicationDbContext()
         : base("name=ProductsDbContext") { }
    public static ApplicationDbContext Create()
    {
        return new ApplicationDbContext();
    }       
}

How should I continue in Configuration.cs Seed() to use ApplicationDbContext in order to store sample user and sample Product in same database defined in ProductsDbContext. 我应如何继续在Configuration.cs Seed()中使用ApplicationDbContext,以便将示例用户和示例产品存储在ProductsDbContext中定义的同一数据库中。

I'm looking more describe to this solution, when database is created initially in one project (domain) than used again in different project and possibly updated with new tables (ui). 当数据库最初在一个项目(域)中创建,而不是在其他项目中再次使用,并可能用新表(ui)更新时,我需要对该解决方案进行更多描述。 Is this proper approach? 这是正确的方法吗? Any example to share? 有什么要分享的例子吗?

If you want 2 DbContexts in 2 separate projects they will always be disjointed. 如果要在2个单独的项目中使用2个DbContext,则它们将始终不相交。

Eg You will not be able to do something like the following: 例如,您将无法执行以下操作:

Context1.Users.Where(u => Context2.Products.Any(p => p.User == u))

So you will loose the ability to use joins across both databases. 因此,您将失去在两个数据库之间使用联接的能力。

One way to seed your data is to use conde first migrations pointing them at both projects, eg 播种数据的一种方法是使用先行迁移,将它们指向两个项目,例如

  • Enable-Migrations on Master Project 在主项目上Enable-Migrations
  • Enable-Migrations on Web.UI Project 在Web.UI项目上Enable-Migrations

In visual studio, in the Package Manager Console, you have an option to choose which project you want to target. 在Visual Studio的Package Manager控制台中,您可以选择要定位的项目。

Then you can add migrations and seed data in the respective projects Configuration.cs . 然后,您可以在各自的项目Configuration.cs添加迁移和种子数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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