在entity framework中使用存储过程

我正在使用entity framework的asp.net mvc 5和C#…我有function的模型和域类…现在我需要使用存储过程….我在运动挣扎。

我正在下面的代码第一个现有的数据库,我有存储过程写在那里。 我的问题是如何在我的Web应用程序中调用该存储过程。

存储过程:

ALTER PROCEDURE [dbo].[GetFunctionByID]( @FunctionId INT ) AS BEGIN SELECT * FROM Functions As Fun WHERE Function_ID = @FunctionId END 

领域类:

  public class Functions { public Functions() { } public int Function_ID { get; set; } public string Title { get; set; } public int Hierarchy_level { get; set; } } 

function模式:

 [Table("Functions")] public class App_Functions { public App_Functions() { } [Key] public int Function_ID { get; set; } [StringLength(50)] [Required] public string Title { get; set; } public int Hierarchy_level { get; set; } //public virtual ICollection<App_Controllers> App_Controllers { get; set; }*/ } 

BaseContext:

 public class BaseContext<TContext> : DbContext where TContext : DbContext { static BaseContext() { Database.SetInitializer<TContext>(null); } protected BaseContext() : base("name = ApplicationDbConnection") { } } 

function上下文:

 public class FunctionsContext : BaseContext<FunctionsContext> { public DbSet<App_Functions> Functions { get; set; } } 

您需要创build一个包含所有存储过程属性的模型类,如下所示。 另外,因为entity framework模型类需要主键,所以可以使用Guid创build一个伪造的键。

 public class GetFunctionByID { [Key] public Guid? GetFunctionByID { get; set; } // All the other properties. } 

然后在你的DbContext注册DbContext模型类。

 public class FunctionsContext : BaseContext<FunctionsContext> { public DbSet<App_Functions> Functions { get; set; } public DbSet<GetFunctionByID> GetFunctionByIds {get;set;} } 

当你调用你的存储过程时,请看下面的内容:

 var functionId = yourIdParameter; var result = db.Database.SqlQuery<GetFunctionByID>("GetFunctionByID @FunctionId", new SqlParameter("@FunctionId", functionId)).ToList()); 

导入存储过程后,可以创build存储过程的对象,传递参数如函数

 using (var entity = new FunctionsContext()) { var DBdata = entity.GetFunctionByID(5).ToList<Functions>(); } 

或者你也可以使用SqlQuery

 using (var entity = new FunctionsContext()) { var Parameter = new SqlParameter { ParameterName = "FunctionId", Value = 5 }; var DBdata = entity.Database.SqlQuery<Course>("exec GetFunctionByID @FunctionId ", Parameter).ToList<Functions>(); } 

您可以使用SqlQuery调用存储过程(请参阅此处 )

 // Prepare the query var query = context.Functions.SqlQuery( "EXEC [dbo].[GetFunctionByID] @p1", new SqlParameter("p1", 200)); // add NoTracking() if required // Fetch the results var result = query.ToList(); 

无意识的乘客有一个项目,允许你从这样的entity framework调用存储过程….

 using (testentities te = new testentities()) { //------------------------------------------------------------- // Simple stored proc //------------------------------------------------------------- var parms1 = new testone() { inparm = "abcd" }; var results1 = te.CallStoredProc<testone>(te.testoneproc, parms1); var r1 = results1.ToList<TestOneResultSet>(); } 

…我正在研究一个存储过程框架 ( 在这里 ),你可以调用像我在下面显示的testing方法之一…

 [TestClass] public class TenantDataBasedTests : BaseIntegrationTest { [TestMethod] public void GetTenantForName_ReturnsOneRecord() { // ARRANGE const int expectedCount = 1; const string expectedName = "Me"; // Build the paraemeters object var parameters = new GetTenantForTenantNameParameters { TenantName = expectedName }; // get an instance of the stored procedure passing the parameters var procedure = new GetTenantForTenantNameProcedure(parameters); // Initialise the procedure name and schema from procedure attributes procedure.InitializeFromAttributes(); // Add some tenants to context so we have something for the procedure to return! AddTenentsToContext(Context); // ACT // Get the results by calling the stored procedure from the context extention method var results = Context.ExecuteStoredProcedure(procedure); // ASSERT Assert.AreEqual(expectedCount, results.Count); } } internal class GetTenantForTenantNameParameters { [Name("TenantName")] [Size(100)] [ParameterDbType(SqlDbType.VarChar)] public string TenantName { get; set; } } [Schema("app")] [Name("Tenant_GetForTenantName")] internal class GetTenantForTenantNameProcedure : StoredProcedureBase<TenantResultRow, GetTenantForTenantNameParameters> { public GetTenantForTenantNameProcedure( GetTenantForTenantNameParameters parameters) : base(parameters) { } } 

如果这两种方法中的任何一种都有好处?

//将一些租户添加到上下文中,以便返回过程的一些东西。 AddTenentsToContext(上下文);

  // ACT // Get the results by calling the stored procedure from the context extention method var results = Context.ExecuteStoredProcedure(procedure); // ASSERT Assert.AreEqual(expectedCount, results.Count); }