我正在使用.NET Core
的最新EF Core 1.1.1
。现在,我想在ApplicationDbContext
类中的OnModelCreating
方法中实现一些通用代码,以调用DB的任何现有SP。请注意,我都有我的模型类别以及数据库表。因此,我也不需要任何迁移,也不希望为OnModelCreating
内的特定SP指定SP参数。它应该是可用于任何SP的通用实现。我的唯一目的是从数据核心调用任何现有的SP。
实体:
public class Product
{
public int Id { get; set; }
public string Name { get; set; }
public decimal Price { get; set; }
}
ApplicationDbContext类:
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);
}
}
我不相信EF Core 1.1迄今支持存储过程。在此处查看EF 6.x与EF Core 1.1的比较:
实体框架功能比较
好的,我发现如果不需要迁移,我们可以跳过OnModelCreating
方法,然后继续使用OnConfiguring
方法。
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;");
}
}