我可以将MVC 5数据库用于asp.net Core应用程序吗?

时间:2019-04-08 12:34:41

标签: c# .net core

我正在将一个相对简单的C#MVC 5应用程序重写为ASP.net Core。我需要在该数据库中使用相同的数据。是否可以使用相同的数据库并通过EF Core轻松连接到该数据库?还是我需要创建一个完整的新数据库并将数据迁移到该新数据库?

3 个答案:

答案 0 :(得分:1)

您可以对任何语言,框架等使用同一数据库,只要该语言支持与该DBMS的连接即可。

数据库不应该知道如何构建使用该数据库的应用程序。其目标是存储数据。您不想为不同的应用程序克隆相同的数据库,这可能最终会导致每个应用程序之间具有不同的数据。

例如:

                             [Mobile application written in swift]
                                                |
                                                |
                                                |
                                                |
                                                V
[Web application written in php] ----------- > [DB] < ----------- [Fat client written in C#]
                                                ^
                                                |
                                                |
                                                |
                                                |
                       [Alien spaceship embedded software written in klingon]

答案 1 :(得分:0)

是的,您可以在Entity Framework Core中由Creating a Model for an Existing Database使用它 使用Scaffold-DbContext基于现有数据库创建模型。可以使用Scaffold-DbContext in Package Manager控制台指定以下参数:

Scaffold-DbContext [-Connection] [-Provider] [-OutputDir] [-Context] [-Schemas>] [-Tables>] 
                    [-DataAnnotations] [-Force] [-Project] [-StartupProject] [<CommonParameters>]

在Visual Studio中,select menu Tools -> NuGet Package Manger -> Package Manger Console并运行以下命令:

PM> Scaffold-DbContext "Server=.\SQLExpress;Database=SchoolDB;Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models

Oficial Docs

Getting-Started-with-Entity-Framework-Core-Database First Development

答案 2 :(得分:0)

It is possible to use EF6 with ASP.NET Core. "The recommended way to use Entity Framework 6 in an ASP.NET Core application is to put the EF6 context and model classes in a class library project that targets the full framework. ".
Here is the documentation with a code sample https://docs.microsoft.com/en-us/aspnet/core/data/entity-framework-6?view=aspnetcore-2.1

Also, Microsoft does not recommend porting from EF6 to EF Core "unless you have a compelling reason to make the change". In case you want to do so, please read the documentation and make sure you validate the requirements. https://docs.microsoft.com/en-us/ef/efcore-and-ef6/porting/