简体   繁体   English

如何从数据库优先方法创建数据访问层对象?

[英]How do I create a Data Access Layer objects from database first approach?

I have built a SQL database with my CustomerDetails, ProductDetails, OrderDetails from scratch and also stored procedures to create new Customers, new Products and Orders.我从头开始使用我的 CustomerDetails、ProductDetails、OrderDetails 以及用于创建新客户、新产品和订单的存储过程构建了一个 SQL 数据库。

So now I want to build a data access layer from C#. I don't know how to associate the entity framework data object declaration such as the following code to map my SQL data.tables, because using the following code will be a code first approach and entity framework will generate the SQL code for me.所以现在我想从C#建立一个数据访问层。我不知道如何将实体框架数据object声明如以下代码关联到map我的SQL data.tables,因为使用以下代码将是一个代码优先方法和实体框架将为我生成 SQL 代码。 I don't want EF generated SQL code because it is so buggy.我不希望 EF 生成 SQL 代码,因为它有很多错误。

public int ProductID { get; set; }
public string ProductName { get; set; }
public string Description { get; set; }
public int? OrderID { get; set; }

I have checked out the following reference which is so daunting for me, because a lot of reference pages are out of date (downloads are no longer available and the User Interface of Visual Studio has been completely changed since), and some are involved in MVC which make things unnecessarily complicated.我查看了以下参考资料,这让我望而生畏,因为很多参考页面都已过时(不再提供下载,Visual Studio 的用户界面已完全更改),有些涉及 MVC这让事情变得不必要的复杂。

https://learn.microsoft.com/en-us/as.net/web-forms/overview/data-access/introduction/ https://learn.microsoft.com/en-us/as.net/web-forms/overview/data-access/introduction/

I have also checked out using Table Adapters.我还检查了使用表适配器。 But it doesn't allow me to create data objects.但它不允许我创建数据对象。 All data objects are automatically generated.所有数据对象都是自动生成的。

So how do I hand-code explicitly both SQL code and data object code if you know what I mean?那么,如果您明白我的意思,我该如何手动显式编码 SQL 代码和数据 object 代码呢? My goal is to be able model my data objects according to the SQL table and call stored procedures already created in SQL to perform all kinds of operations.我的目标是能够model我的数据对象根据SQL表调用SQL中已经创建的存储过程进行各种操作。 Please let me know where I can take reference.请让我知道我可以参考的地方。

From what I saw you are trying to do databasefist approach so you need to do the following things:据我所见,您正在尝试采用 databasefist 方法,因此您需要执行以下操作:

First you need to open the package manager console.首先您需要打开 package 管理控制台。 That is from那是从
View -> Other Windows -> Package Manager Console.查看 -> 其他 Windows -> Package 管理器控制台。
There you need to make sure that the default project is the project you want to work with.您需要确保默认项目是您要使用的项目。

像这样

Then run this command然后运行这个命令

Scaffold-DbContext -Provider Microsoft.EntityFrameworkCore.SqlServer 
                   -Connection "Data Source=.\SQLExpress;Initial Catalog=DatabaseName;Integrated Security=True"

Change the "Data Source" to your SQL instance and "Initial Catalog" with your database name将“数据源”更改为您的 SQL 实例,将“初始目录”更改为您的数据库名称

(This will work only if you use SqlServer) (这仅在您使用 SqlServer 时有效)

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

相关问题 使用 Entity Framework Core 数据库优先方法如何将我的实体与基础设施层分开? - With Entity Framework Core database first approach how do I separate my entity from infrastructure layer? 如何使用带有初始化程序的代码优先方法创建数据库 - How to create database with code first approach with initializer 如何创建数据访问层 - How to create Data access layer 如何从对象数组访问数据? - How do I access the data from an array of objects? 如何正确设计数据访问层? - How do I design a Data Access Layer appropriately? 我应该在业务层还是数据访问层新建数据库 model - Should i new database model in business layer or data access layer 如果我在实体框架代码第一方法中从 static 方法保存数据,为什么记录没有保存在数据库中? - Why record is not being saved in database if i save data from static method in entity framework code first approach? 如何使用实体框架代码优先方法正确创建数据库 - How to correctly create a database using entity framework code first approach 如何创建数据库优先方法精确类项目 - How to create database first approach exact class project ASP.Net中的数据访问层:我在哪里创建连接? - Data Access Layer in ASP.Net: Where do I create the Connection?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM