Reputation: 17288
I have 3 tables; I write a stored procedure in 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!!!. How can i use if i join 3 table before?
Upvotes: 6
Views: 10024
Reputation: 37633
and here http://www.danrigsby.com/blog/index.php/2009/05/20/entity-framework-40-scalar-and-void-functions/
Upvotes: 0
Reputation: 754468
Okay, you need a few steps here:
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)DepartmentDataEntityType
or something; add all the fields being returned from the stored procedure to that entity typeDepartmentDataEntityType
from above)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. 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. 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 :)
Upvotes: 6
Reputation:
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.
Upvotes: 1