0

I have been really excited about using ASP.NET Core 1.0 and can't wait to create some production apps. The one issue I have is the Data Access, using MVC5 and EF6 made it really easy to create an .edmx file and have your Stored Procedures mapped (complex types), a fantastic feature which makes it really easy for us to access data.

EF Core 1.0 github page says that it will implement Stored procedure mapping.

I have seen examples where you write your query but I want to prevent SQL Injection.

What is the best way to call a Stored Procedures with EF6 using .NET Core 1.0 with the full .NET framework?

Community
  • 1
  • 1
shammelburg
  • 6,974
  • 7
  • 26
  • 34
  • I think DbContext.Database.SqlQuery it the way to go - or if you have more to execute than stored procs you can try https://codefirstfunctions.codeplex.com/ I wrote. Here is more details: https://blog.3d-logic.com/2014/04/09/support-for-store-functions-tvfs-and-stored-procs-in-entity-framework-6-1/ Caveat: I have not tried it in ASP.NET Core word but I don't see why it wouldn't work if you target full framework. – Pawel Jul 07 '16 at 00:16

1 Answers1

0

One possible work around is to use a Class Library which you can add your EF6 ADO.NET Entity Data Model to.

Class Library (DataAccessLayer)

Add your connection string data here

namespace DataAccessLayer
{
    public class Class1
    {
        public static DbConnection GetConnectionString()
        {
            EntityConnectionStringBuilder entityBuilder = new EntityConnectionStringBuilder();
            entityBuilder.Metadata = "res://*/Models.devModel.csdl|res://*/Models.devModel.ssdl|res://*/Models.devModel.msl";
            entityBuilder.ProviderConnectionString = "data source=dev-server;initial catalog=ProductWorkflow_New;persist security info=True;user id=user;password=password;MultipleActiveResultSets=True;App=EntityFramework";
            entityBuilder.Provider = "System.Data.SqlClient";
            return new EntityConnection(entityBuilder.ToString());
        }
    }
}

devModel.Context.cs (devModel.edmx)

public partial class devModelEntities : DbContext
{
    public devModelEntities()
        // Add static method here
        : base(Class1.GetConnectionString(), true)
    {
    }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        throw new UnintentionalCodeFirstException();
    }

    // Your generated code..

}
shammelburg
  • 6,974
  • 7
  • 26
  • 34