I'm using the latest EF Core 1.1.1
with .NET Core
. Now I want to implement some generic code inside OnModelCreating
method in my ApplicationDbContext
class to call any existing SP from db. Please note, I have both my model classes as well as database tables. So neither do I want any migration nor want to specify SP params for a particular SP inside OnModelCreating
. It should be a generic implementation that can be used for any SP. My sole purpose is to call any existing SP from databse.
Entity:
public class Product
{
public int Id { get; set; }
public string Name { get; set; }
public decimal Price { get; set; }
}
ApplicationDbContext Class:
public class ApplicationDbContext : DbContext
{
public ApplicationDbContext ()
{
Database.Connection.ConnectionString = "Data Source=.\\SQLExpress;Initial
Catalog=CallingExistingSPFromEFCore;Integrated Security=True";
}
public DbSet<Product> Products { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Product>().MapToStoredProcedures
(
//What should be the implementation here ?
)
base.OnModelCreating(modelBuilder);
}
}
OK guys so I found that in case migration is not needed, We can skip the OnModelCreating
method and just go ahead with OnConfiguring
method.
public class ApplicationDbContext : DbContext
{
public DbSet<Student> Students { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlServer(@"Server=servername;Database=dbname;Trusted_Connection=True;");
}
}
I do not believe EF Core 1.1 supports Stored Procedures as of yet. Check out the comparison of EF 6.X vs EF Core 1.1 here: