首页 > 解决方案 > Entity Framework working with SQL Server views for ASP. NET web api

问题描述

I am researching my first RESTful API project to interact with the automation side of our ERP system.

I am planning a SPA like here

The simple web page is to provide users with a list of purchase requisitions they must approve or decline.

The app will retrieve the data from a SQL Server view into a collection. As the user approves or declines the requisition this is recorded in the object and when the User presses 'Update ERP' the app will build a XML list of the collection and using a SQL Server stored procedure insert it into the SQL database of the ERP for the automation process to pick up and process

I have three questions

  1. The examples uses SQL Server tables in EF should I build the view in the "model" (some how!) or can I use my SQL Server view?

  2. If I use my SQL Server view, does it need an ID / unique key column (for example row number) to make EF work better?

  3. Would be better using a C# class to do the data retrieval and sending the xml data into the database with SQL Server stored procedure than EF?

标签: asp.netsql-serverentity-frameworkasp.net-web-api2

解决方案


该示例在 EF 中使用 SQL 表,我应该在“模型”中构建视图(某种方式!)还是可以使用我的 SQL 视图?

使用“ Code First to an Existing Database ”工作流程,您可以从数据库中选择视图,EF 将生成类。

如果我使用我的 SQL 视图,它是否需要一个 ID/唯一键列(例如行号)才能使 EF 更好地工作?

EF 将尝试识别关键列,但它不是很聪明。您可以在初始生成后更改模型。如果您不更新实体,密钥并不重要,但每个实体仍然需要一个密钥。

使用 C# 类进行数据检索并使用 SQL 存储过程将 xml 数据发送到数据库中会比 EF 更好吗?

不,我会使用 EF 进行检索,但可能直接使用 ADO.NET 来调用存储过程。


推荐阅读