简体   繁体   中英

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.

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. I don't want EF generated SQL code because it is so buggy.

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.

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? 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. 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:

First you need to open the package manager console. That is from
View -> Other Windows -> Package Manager Console.
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

(This will work only if you use SqlServer)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM