I do mapping here for tables.. How to map stored procedures here?
public class AppDBContext : DbContext
{
public DbSet<UserAccount> UserAccount { get; set; }
public DbSet<GetUserAccounts> GetGetUserAccounts { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<UserAccount>().ToTable("UserAccount");
modelBuilder.Entity<Customer>().ToTable("Customer");
base.OnModelCreating(modelBuilder);
}
}
Code first does not support stored procedures.
You can execute scripts in a database initialiser using:
string sql = "CREATE PROCEDURE [MyProc]...";
context.Database.ExecuteSqlCommand(sql);
You can execute procedures them from the context like so:
string command = "EXEC MyProc";
IEnumerable<T> results = context.Database.SqlQuery<T>(command, null);
Personally, I wrap this up into a nice OO model. I have specialised SP class with strongly typed methods. These methods are decorated with an attribute that tells the DB initialiser to create a stored procedure of a given name from a given source. The strong type methods call the stored procedure.