简体   繁体   English

如何在ADO.NET Entity Framework中使用存储过程

[英]How to use a stored procedure in ADO.NET Entity Framework

I have 3 tables; 我有3张桌子; I write a stored procedure in ADO.NET Entity Framework. 我在ADO.NET Entity Framework中编写了一个存储过程。

ALTER PROCEDURE [dbo].[sp_GetDepartmanData]
(@departman nvarchar(50))
BEGIN
  SELECT  
    d.ID, d.Name as DepartmanName,  
    sb.Salary, sb.email,
    sp.Name, sp.SurName, sp.Phone, sp.Married, sp.Address
  FROM         
    Departman d 
  INNER JOIN StaffsBusiness sb ON d.ID = sb.StaffsPersonelDepartmanID
  INNER JOIN StaffsPersonel sp ON sb.StaffsPersonelID = sp.ID 
  WHERE
    d.Name = @departman
END

I need a stored procedure function I write below: 我需要一个我在下面写的存储过程函数:

var staffPersonel = staffContext.GetPersonelInformationWithDepartmanID("Yazılım");

gvPersonel.DataSource = staffPersonel;
gvPersonel.DataBind();

GetPersonelInformationWithDepartmanID function I write from SQL (user defined function in ADO.NET Entity Framework) there are 3 alternative (it is silly!!!) but i have 3 joininig table!!!. GetPersonelInformationWithDepartmanID函数我从SQL编写(ADO.NET实体框架中的用户定义函数)有3个替代方案(很傻!!!)但我有3个joininig表!!! How can i use if i join 3 table before? 如果我之前加入3桌,我该如何使用?

Okay, you need a few steps here: 好的,你需要在这里做几步:

  • add your stored procedure sp_GetDepartmanData to your Entity Framework model (as an aside - it's is strongly recommend NOT to call your stored procedures sp_(something) - use of the sp_ prefix is reserved for Microsoft-only system stored procedures) 将存储过程sp_GetDepartmanData添加到您的Entity Framework模型中(另外 - 强烈建议不要调用存储过程sp_(something) - 使用sp_前缀保留给仅限Microsoft的系统存储过程)
  • since your stored procedure is returning a set of data, you will need to create a conceptual entity for it first, before you can use your stored proc; 由于存储过程返回一组数据,因此在使用存储过程之前,首先需要为它创建一个概念实体; in the Entity Designer, create a new entity and call it some useful name like DepartmentDataEntityType or something; 在实体设计器中,创建一个新实体并将其称为一些有用的名称,如DepartmentDataEntityType或其他; add all the fields being returned from the stored procedure to that entity type 将从存储过程返回的所有字段添加到该实体类型
  • now, you can create your function import in the entity data model - go to the model browser, in the "model.store" section go to your stored procedure, and right-click on "create function import" 现在,您可以在实体数据模型中创建函数导入 - 转到模型浏览器,在“model.store”部分转到存储过程,然后右键单击“创建函数导入”
  • you can now give your function in the object context a name and define what it returns - in this case, pick your newly created entity type (eg DepartmentDataEntityType from above) 你现在可以在对象上下文中为你的函数命名并定义它返回的内容 - 在这种情况下,选择你新创建的实体类型(例如上面的DepartmentDataEntityType
  • you're done! 你完成了!

You should now have a function import something like: 你现在应该有一个函数导入类似于:

public global::System.Data.Objects.ObjectResult<DepartmentDataEntityType> GetPersonelInformationWithDepartmanID(global::System.String departmentName)
{
    global::System.Data.Objects.ObjectParameter departmentNameParameter;

    departmentNameParameter = new global::System.Data.Objects.ObjectParameter("departmentNameParameter", departmentName);

    return base.ExecuteFunction<DepartmentDataEntityType>("sp_GetDepartmanData", departmentNameParameter);
}

This function on your object context can now be call to retrieve the data via the stored procedure from your database. 现在可以调用对象上下文中的此函数,以通过数据库中的存储过程检索数据。

Marc

Edit: 编辑:

If you are getting a mapping error ("Error 3027: No mapping specified for the following EntitySet/AssociationSet") after doing this, it's because the entity you created is not mapped to anything and is only ever used when the function import populates a collection of these entities. 如果您在执行此操作后收到映射错误(“错误3027:未为以下EntitySet / AssociationSet指定映射”),那是因为您创建的实体未映射到任何内容,并且仅在函数导入填充集合时使用这些实体 You either need to map this entity to a data store somehow or you need to change it to a complex type. 您需要以某种方式将此实体映射到数据存储,或者您需要将其更改为复杂类型。

To create a complex type simply open up the EF designer and right-click on an empty area. 要创建复杂类型,只需打开EF设计器并右键单击空白区域。 Go to Add > Complex Type. 转到添加>复杂类型。 You should see a new complex type appear in the model browser. 您应该会在模型浏览器中看到一个新的复杂类型。 Right click it and add scalar properties similar to how you added properties to your entity. 右键单击它并添加类似于向实体添加属性的标量属性。 Then delete your entity and rename your complex type the same as the entity. 然后删除您的实体并重命名您的复杂类型与实体相同。

That's all you have to do :) 这就是你所要做的:)

How do you create this "conceptual entity"? 你如何创建这个“概念实体”? If I create an entity which is not mapped to the I get the following error: "Entity type 'foobar' is not mapped to the database. 如果我创建一个未映射到的实体,我会收到以下错误:“实体类型'foobar'未映射到数据库。

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

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