实体框架数据库优先.Net核心 [英] Entity Framework Database First .Net Core

查看:94
本文介绍了实体框架数据库优先.Net核心的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个.Net Standard 2.0类库项目,我想向其中添加实体框架.我已将Entity Framework Core软件包添加到项目中,但是当我尝试向项目中添加新项目时没有出现Entity Framework Templates.我在网上浏览了一下,似乎只能找到有关Code First的说明!

I have a .Net Standard 2.0 class library project and I want to add Entity Framework to it. I have added the Entity Framework Core package to the project but no Entity Framework Templates appear when I try to add a new item to the project. I have looked on the web and all I can seem to find is instructions for Code First!

如何重新获得数据库优先功能?

How do I get the database first functionality back?

推荐答案

EF Core不会(也永远不会)支持设计人员基于EDMX的Database First工作流. EF Core将所有对象到数据库的映射存储在源代码的属性"中,并将Fluent API映射存储在源代码中.

EF Core does not, and will never, support the EDMX-based Database First workflow with the designer. EF Core stores all object-to-database mapping in Attributes and Fluent API mapping in your source code.

在EF 6中,代码优先"一词意味着两个截然不同的事物.一个是代码优先的建模工作流,其中数据库是从.NET类生成的. 代码优先"的另一个含义是,映射元数据嵌入在您的源代码(Attributes/Fluent API)中,而不是在EDMX文件中. EF 6支持两个不同数据库优先工作流程. EDMX是数据库优先的,工作流正式称为来自现有数据库的代码优先",但也可以称为基于代码的映射的数据库优先".

In EF 6 the term "Code First" meant two very different things. One is a code-first modeling workflow where your database was generated from your .NET classes. The other meaning of "Code First" was just that the mapping metadata was embedded in your source code (Attributes/Fluent API) rather than in an EDMX file. EF 6 supported two different database-first workflows. Database-first with the EDMX, and the workflow officially called "Code First From an Existing Database", but which could have been called "Database-First with Code-Based Mapping".

在EF Core中,您的代码将始终具有映射关系,因此从某种意义上讲,它是代码优先".但是您仍然可以执行数据库优先的设计工作流,并编写与现有数据库匹配的实体和映射代码.

In EF Core, your code will always have the mapping, and so in that sense it's "code first". But you can still do a database-first design workflow, and write entities and mapping code that match your existing database.

并且您可以使用Package Manager控制台中的scaffold-dbcontext或CLI命令中的dotnet ef dbcontext scaffold来从现有数据库生成实体类和映射元数据.请参见 ASP.NET Core上的EF Core入门使用现有数据库

And you can use the scaffold-dbcontext in the Package Manager Console, or dotnet ef dbcontext scaffold in the CLI command to generate entity classes and mapping metadata from an existing database. See Getting Started with EF Core on ASP.NET Core with an Existing Database

这篇关于实体框架数据库优先.Net核心的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆