none
About Entity Framework code first RRS feed

  • Question

  • Hello,

    Im a beginner in entity framework code first.

    1.
    Is it possible to execute a a SP in in the latest version of entity framework (Code first)?

    You need to send two variable in c# to the SP (for insert or delete in sql) as a argument.

    2.
    How flexible is EF code first in relation to repository?

    3.
    Do you know a tutorial for beginners in relations to EF code first for executing SP?

    Thank you!
    Monday, April 10, 2017 9:41 PM

Answers

  • Hi Sakura Data,

    >>Is it possible to execute a a SP in in the latest version of entity framework (Code first)?

    Yes, We could use Raw SQL to achieve it. like this:

    #Store Procedure

    CREATE PROCEDURE [dbo].[Blog_Insert]
        @Name [nvarchar](max),
        @Url [nvarchar](max)
    AS
    BEGIN
        INSERT [dbo].[Blog]([Name], [Url])
        VALUES (@Name, @Url)
    END

    #Usage:

    using (var db = new BloggingContext())
                {
                    var blog = new Blog() { Name = "Te", Url = "Ul" };
                    db.Database.ExecuteSqlCommand("Blog_Insert @Name, @Url",
                        new SqlParameter("@Name", blog.Name),
                        new SqlParameter("@Url", blog.Url)
                    );
                }

    >>How flexible is EF code first in relation to repository?

    You Could also use Raw SQL with Code first as your want.

    >>Do you know a tutorial for beginners in relations to EF code first for executing SP?

    For more information about Raw SQL, please refer to:

    https://msdn.microsoft.com/en-us/library/jj592907(v=vs.113).aspx

    about CURD with Stored Procedure, please refer to:

    https://msdn.microsoft.com/en-us/library/dn468673(v=vs.113).aspx

    Best regards,

    Cole Wu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Sakura Data Tuesday, April 11, 2017 4:15 PM
    Tuesday, April 11, 2017 8:32 AM
    Moderator

All replies

  • Hi Sakura Data,

    Thank you for posting here.

    According to your question is more related to Entity Framework, I will move it to ADO.NET Entity Framework and LINQ to Entities forum for suitable support.

    The Visual C# discuss and ask the C# programming language, IDE, libraries, samples and tools.

    If you have some grammar or code errors, please feel free to contact us. We will try our best to give you a solution.

    Thanks for your understanding and cooperation.

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, April 11, 2017 2:15 AM
  • Hi Sakura Data,

    >>Is it possible to execute a a SP in in the latest version of entity framework (Code first)?

    Yes, We could use Raw SQL to achieve it. like this:

    #Store Procedure

    CREATE PROCEDURE [dbo].[Blog_Insert]
        @Name [nvarchar](max),
        @Url [nvarchar](max)
    AS
    BEGIN
        INSERT [dbo].[Blog]([Name], [Url])
        VALUES (@Name, @Url)
    END

    #Usage:

    using (var db = new BloggingContext())
                {
                    var blog = new Blog() { Name = "Te", Url = "Ul" };
                    db.Database.ExecuteSqlCommand("Blog_Insert @Name, @Url",
                        new SqlParameter("@Name", blog.Name),
                        new SqlParameter("@Url", blog.Url)
                    );
                }

    >>How flexible is EF code first in relation to repository?

    You Could also use Raw SQL with Code first as your want.

    >>Do you know a tutorial for beginners in relations to EF code first for executing SP?

    For more information about Raw SQL, please refer to:

    https://msdn.microsoft.com/en-us/library/jj592907(v=vs.113).aspx

    about CURD with Stored Procedure, please refer to:

    https://msdn.microsoft.com/en-us/library/dn468673(v=vs.113).aspx

    Best regards,

    Cole Wu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Sakura Data Tuesday, April 11, 2017 4:15 PM
    Tuesday, April 11, 2017 8:32 AM
    Moderator
  • I have a follow up question.

    CREATE PROCEDURE [dbo].[GetBlog]
    AS
    BEGIN
        select a, b, c
        from data
    END


    How whould you write the code in order to retrieve a big list (Ilist<Blog>) from the SP in relation to Raw SQL ?

    Thank you!
    • Edited by Sakura Data Tuesday, April 11, 2017 4:32 PM
    Tuesday, April 11, 2017 4:30 PM