Answer the question
In order to leave comments, you need to log in
Stored procedure from c# code in EF(Code First)?
There is a data context and a data initializer,
public class ShoopProductContext: DbContext
{
public DbSet<Client> Clients { get; set; }
public DbSet<OrderLine> OrderLines { get; set; }
public DbSet<Order> Orders { get; set; }
public DbSet<Product> Products { get; set; }
}
public class shoopProductsInitializer : DropCreateDatabaseAlways<ShoopProductContext>
{
protected override void Seed(ShoopProductContext context)
{
context.Products.Add(new Product { NameProduct = "11" });//Тут должна быть ХП
context.SaveChanges();//
}
}
Answer the question
In order to leave comments, you need to log in
Stored Procedure in Entity Framework
Stored Procedures Stored procedures
can be created by migration, and it will be 100% on every database.
I think everything can be done in storage, the main thing is to return a normal object.
1 and 2 - register in migration
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.Sql(@"CREATE PROC dbo.Execute ...");
}
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.Sql(@"DROP PROC dbo.Execute");
}
public class MyDbContext: DbContext
{
public IEnumerable<TEntity> ExecuteSql<TEntity>(string query, dynamic parameters = null, CommandType commandType = CommandType.Text)
{
using (SqlConnection connection = new SqlConnection(Database.GetDbConnection().ConnectionString))
{
connection.Open();
return connection.Query<TEntity>(query, (object)parameters, commandType: commandType);
}
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question