简体   繁体   English

.NET Core Entity Framework 如何从模型创建表

[英]How does .NET Core Entity Framework Create Tables From Model

I'm new to .NET Core , and I am exploring this particular tutorial: https://docs.microsoft.com/en-us/aspnet/core/tutorials/first-mvc-app/adding-model?view=aspnetcore-2.2&tabs=visual-studio我是.NET Core新手,我正在探索这个特定的教程: https : //docs.microsoft.com/en-us/aspnet/core/tutorials/first-mvc-app/adding-model?view=aspnetcore -2.2&tabs=visual-studio

Now it seems the Entity Framework takes a code first approach to generate the database tables from the created model, and the tutorial uses this scaffolding thing to create the views and controller before generating the tables via the package manager console.现在, Entity Framework似乎采用代码优先的方法从创建的模型生成数据库表,并且本教程使用这个脚手架在通过包管理器控制台生成表之前创建视图和控制器。

My question is: I was wondering how does the solution really know which models are to be pushed to the database on Update-Database ?我的问题是:我想知道解决方案如何真正知道将哪些模型推送到Update-DatabaseUpdate-Database I have added some standalone model DBSets to the DBContext manually (in the tutorial MvcMovieContext), but that didn't seem to work.我手动(在教程 MvcMovieContext 中)向DBContext添加了一些独立模型DBSets ,但这似乎不起作用。 Is it the Migrations folder?Migrations文件夹吗? How does it work?它是如何工作的?

I would think that this scaffolding step isn't necessary (especially if I would like to maintain the tables without any UI interfaces), and that it has some kind of list somewhere that says which model object goes into database or not.我认为这个脚手架步骤不是必需的(特别是如果我想在没有任何 UI 界面的情况下维护表),并且它在某处有某种列表,说明哪个模型对象进入数据库。

Entity Framework (Core) works the following: By creating a DbContext or any class that derives from it, EF Core checks for DbSet s and their related classes in the code.实体框架 (Core) 的工作方式如下:通过创建DbContext或从它派生的任何类,EF Core 在代码中检查DbSet及其相关类。 Take for example this DbContext :以这个DbContext

public class StackOverflowDbContext : DbContext {
    public DbSet<MyClass> Test { get; set; }
}

As soon as you start with your initial migration (can be done for example via dotnet ef migrations add Initial ), EF checks for a DbContext class.一旦您开始初始迁移(例如可以通过dotnet ef migrations add Initial ),EF 会检查DbContext类。 If there are multiple, you need to specify that, otherwise it takes the first available and analyses it.如果有多个,则需要指定,否则取第一个可用并进行分析。 In this case, the MyClass needs to be added to the database and therefore the class and all it's properties start to appear in the initial migration.在这种情况下,需要将MyClass添加到数据库中,因此该类及其所有属性开始出现在初始迁移中。

From there, you can update your model whenever you want, but do not forget to create a new migration after that.从那里,您可以随时更新您的模型,但不要忘记在此之后创建一个新的迁移。

I would think that this scaffolding step isn't necessary我认为这个脚手架步骤是没有必要的

And yes, that is true.是的,这是真的。 You don't need to use scaffolding, it's just there for providing a starting point.您不需要使用脚手架,它只是提供一个起点。

You must to create a migrations and update your db.您必须创建迁移并更新您的数据库。 You can find commands here http://www.entityframeworktutorial.net/efcore/entity-framework-core-migration.aspx您可以在这里找到命令http://www.entityframeworktutorial.net/efcore/entity-framework-core-migration.aspx

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

相关问题 实体框架不会为多对多表创建模型实体 - Entity Framework does not create model entity for many to many tables 从不同的表创建 ASP Net Core Entity Framework 实体 - ASP Net Core Entity Framework entity creation from different tables 如何从不同的表中获取特定的列 ASP.NET 核心和实体框架核心 - How to get specific columns from different tables ASP.NET Core and Entity Framework Core 如何从两个表中获取数据并将其保存在Net Core Entity Framework的DTO中? - How to get data from two tables and save it in DTO in Net Core Entity Framework? 如何仅获取特定表以使用 C# 中的 Entity Framework Core 从数据库创建模型 - How to get only specific tables to create models from database with Entity Framework Core in C# 如何将 model 传递给视图 .NET 核心实体框架 - How pass a model to a view .NET Core Entity Framework 使用 .net core Entity Framework 从 JSON 创建嵌套数据 - Create nested data from JSON using .net core Entity Framework 如何对 dotnet core Entity Framework 中的许多表中的数据进行分组 - How to GroupBy data from many tables in dotnet core Entity Framework 如何在ASP.NET实体框架中的2个表之间创建关系 - How to create relationship between 2 tables in ASP.NET Entity Framework 如何使用 Net core 3 和 Entity Framework 由新客户端创建新数据库? - How to create new database by new client with Net core 3 and Entity Framework?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM