Execute StoredProcedure in CodeFirst 4.1

不羁岁月 提交于 2019-12-07 05:55:44

问题


I understand stored procedures mapping is not supported by my understanding is that I should be able to call stored procedures.

I have quite a few complex stored procedures and with the designer I could create a complex type and I was all good.

Now in code first let's suppose I have the following stored procedure, just put together something silly to give an idea. I want to return a student with 1 address.

In code I have A Student and Address Entity. But no StudentAddressEntity as it's a link table.

I have tried the following but I get an error

Incorrect syntax near '."}
System.Data.Common.DbException {System.Data.SqlClient.SqlException}

ALTER Procedure [dbo].[GetStudentById]
   @StudentID int
AS
   SELECT  *
   FROM Student S
   left join StudentAddress SA on S.Studentid = sa.studentid
   left join Address A on SA.AddressID = A.AddressID
   where S.StudentID = @StudentID

C# code:

using (var ctx = new SchoolContext())
{
   var student = ctx.Database.SqlQuery<Student>("GetStudentById,@StudentID",
                                                new SqlParameter("StudentID", id));
}

Any examples out there how to call sp and fill a complexType in code first, using out parameters etc.. Can I hook into ADO.NET?

Trying just an SP that returns all students with no parameters I get this error

System.SystemException = Cannot create a value for property 'StudentAddress' of type 'CodeFirstPrototype.Dal.Address'. Only properties with primitive types are supported.

Is it because I have in a way ignore the link table?

Any suggestions?


回答1:


I believe that your exception actually is:

Incorrect syntax near ','.

because this is invalid statement: "GetStudentById,@StudentID". It should be without comma: "GetStudentById @StudentID".

The problem with stored procedures in EF is that they don't support loading navigation properties. EF will materialize only the main entity and navigation properties will not be loaded. This is solved for example by EFExtensions. EFExtensions are for ObjectContext API so you will have to check if it is also usable for DbContext API.




回答2:


Using EFExtentions it will look something like

using (var context = new SchoolContext())
{
    var command = context.CreateStoreCommand("GetStudentById", CommandType.StoredProcedure,
      new SqlParameter("StudentID", id));

    using (command.Connection.CreateConnectionScope())
    using (var reader = command.ExecuteReader())
    {
        // use the reader to read the data
        // my recommendation is to create a Materializer using EFExtensions see 
        // http://blogs.msdn.com/b/meek/archive/2008/03/26/ado-entity-framework-stored-procedure-customization.aspx

        // ex
        var student = Student.Materializer.Materialize(reader).SingleOrDefault();

        return student;
   }
}


来源:https://stackoverflow.com/questions/5776918/execute-storedprocedure-in-codefirst-4-1

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!