我应该如何使用新的类库nuget包设计我的visual studio解决方案

时间:2022-06-05 07:01:26

I have 2 folders:

我有2个文件夹:

My-WebApplication
My-WebService

Both are 2 asp.net mvc 6.0 project in an extra visual studio solution.

这两个都是2个asp.net mvc 6.0项目的额外视觉工作室解决方案。

Both need to share a custom DbContext class using Entity Framework 7.

两者都需要使用Entity Framework 7共享自定义DbContext类。

I use Visual Studio 2015 and develop against

我使用Visual Studio 2015进行开发

"frameworks" : { "net451" }

Where would you create the custom DbContext class and how would you share it among both VS solutions using the latest features of Visual Studio 2015 concerning class libraries as nuget packages - if that helps -

您将在哪里创建自定义DbContext类,以及如何使用Visual Studio 2015的最新功能将类库作为nuget包在VS解决方案中进行共享 - 如果有帮助的话 -

OR

要么

should I just create a class library and put that in a 3rd share.sln file in a 3rd folder on the same level as the others? and both solutions reference then this class library?

我应该创建一个类库,并将其放在第三个文件夹中的第三个share.sln文件中与其他文件夹在同一级别上?并且这两个解决方案都参考了这个类库?

1 个解决方案

#1


0  

You can use a common DbContext in a separate assembly (package class library) . DbContext is initiated separately for each project in ConfigureServices of Startup.cs

您可以在单独的程序集(程序包类库)中使用常见的DbContext。在Startup.cs的ConfigureServices中为每个项目单独启动DbContext

services.AddEntityFramework()
            .AddSqlServer()
            .AddDbContext<YourDbContext>(options => options.UseSqlServer(Configuration["Data:ConnectionString"]));

It is necessary to specify dependencies for each project in project.json

有必要在project.json中为每个项目指定依赖项

"dependencies": {
    ...
    "YourProject.Data": "1.0.0.0",
    ...
}

where YouProject.Data is an assembly with YourDbContext.

其中YouProject.Data是具有YourDbContext的程序集。

Also you need to add dependencies and the command in YouProject.Data project.json for a configuration of migrations for your context:

您还需要在YouProject.Data project.json中添加依赖项和命令,以获取上下文的迁移配置:

"dependencies": {
    "EntityFramework.SqlServer": "7.0.0-beta7",
    "EntityFramework.Commands": "7.0.0-beta7"
}
"commands": {
    "ef": "EntityFramework.Commands"
},

You can use the next command for a creation of new migration (for MVC6 beta-7)

您可以使用下一个命令创建新的迁移(适用于MVC6 beta-7)

dnx ef migrations add NameOfMigraion -c YourDbContext -s YouApplicationProjectName

dnx ef migrations添加NameOfMigraion -c YourDbContext -s YouApplicationProjectName

where YouApplicationProjectName is a name of your asp.net mvc6 project name, for example My-WebApplication or My-WebService

其中YouApplicationProjectName是您的asp.net mvc6项目名称的名称,例如My-WebApplication或My-WebService

I hope this will help you.

我希望这能帮到您。

#1


0  

You can use a common DbContext in a separate assembly (package class library) . DbContext is initiated separately for each project in ConfigureServices of Startup.cs

您可以在单独的程序集(程序包类库)中使用常见的DbContext。在Startup.cs的ConfigureServices中为每个项目单独启动DbContext

services.AddEntityFramework()
            .AddSqlServer()
            .AddDbContext<YourDbContext>(options => options.UseSqlServer(Configuration["Data:ConnectionString"]));

It is necessary to specify dependencies for each project in project.json

有必要在project.json中为每个项目指定依赖项

"dependencies": {
    ...
    "YourProject.Data": "1.0.0.0",
    ...
}

where YouProject.Data is an assembly with YourDbContext.

其中YouProject.Data是具有YourDbContext的程序集。

Also you need to add dependencies and the command in YouProject.Data project.json for a configuration of migrations for your context:

您还需要在YouProject.Data project.json中添加依赖项和命令,以获取上下文的迁移配置:

"dependencies": {
    "EntityFramework.SqlServer": "7.0.0-beta7",
    "EntityFramework.Commands": "7.0.0-beta7"
}
"commands": {
    "ef": "EntityFramework.Commands"
},

You can use the next command for a creation of new migration (for MVC6 beta-7)

您可以使用下一个命令创建新的迁移(适用于MVC6 beta-7)

dnx ef migrations add NameOfMigraion -c YourDbContext -s YouApplicationProjectName

dnx ef migrations添加NameOfMigraion -c YourDbContext -s YouApplicationProjectName

where YouApplicationProjectName is a name of your asp.net mvc6 project name, for example My-WebApplication or My-WebService

其中YouApplicationProjectName是您的asp.net mvc6项目名称的名称,例如My-WebApplication或My-WebService

I hope this will help you.

我希望这能帮到您。