32

私はasp.net mvc 5とC#をEntity Frameworkで使用しています...関数用のモデルおよびドメイン・クラスがあります...今はストアド・プロシージャを使用する必要があります。動きに苦しんでいる。エンティティ・フレームワーク内のストアド・プロシージャを使用

私はコードの最初の既存のデータベースに従っており、そこに書かれたプロシージャを保存しています。私の質問は、私のWebアプリケーションでそのストアドプロシージャを呼び出す方法です。

ストアドプロシージャ:

​​

ドメインクラス:

public class Functions 
{ 
    public Functions() 
    { 
    } 

    public int Function_ID { get; set; } 
    public string Title { get; set; } 
    public int Hierarchy_level { get; set; } 
} 

機能モデル:

[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") 
    { } 
} 

関数コンテキスト:

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

答えて

38

あなたは以下のようにすべてのストアドプロシージャのプロパティが含まれているモデルクラスを作成する必要があります。 また、Entity Frameworkモデルクラスにプライマリキーが必要なため、Guidを使用して偽のキーを作成できます。その後、

public class GetFunctionByID 
{ 
    [Key] 
    public Guid? GetFunctionByID { get; set; } 

    // All the other properties. 
} 

あなたDbContextGetFunctionByIDモデルクラスを登録します。

あなたのストアドプロシージャを呼び出す
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()); 
+0

がデバッグ中に結果に対してNULL値を取得しています! – toxic

+0

多くのありがとう、それは働く.... – toxic

+0

私はそれが助けてうれしいです。 – Lin

8

あなたはSqlQueryを使用してストアドプロシージャを呼び出すことができます(hereを参照)

// 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(); 
+0

の内側にあなたのストアドプロシージャ内の任意のパラメータを追加することができ、この平均値は、I dbcontextの各ストアプロシージャとdbsetのモデルを作成する必要はありませんか? – toxic

+0

デバッグ中にnullの結果が出ます!!! – toxic

+0

@ toxicあなたは '200'をデータベースのidのidに変更しましたか? – qujck

8

ストアドプロシージャをインポートした後、ストアドプロシージャのオブジェクトを作成することができる機能

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>(); 
} 
0

Mindless passengerあなたは、このようなエンティティフレームワークからストアドプロシージャを呼び出すことができますプロジェクト....

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>(); 
} 

を持って...と私はあなたのよう呼び出すことができますstored procedure frameworkhere)に取り組んでいます下に示す私のテスト方法の1つで...

[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) 
    { 
    } 
} 

これらの2つのアプローチのいずれかが良い場合は、

0

//状況に応じてテナントを追加して、手続きのために何かを用意してください! 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); 
} 
+0

// ACT //結果を取得コンテキストエクステンションメソッドからストアドプロシージャを呼び出すことによって実現します。 var results = Context.ExecuteStoredProcedure(procedure); // ASSERT Assert.AreEqual(expectedCount、results。Count ++); } – gsgsgs

+1

何かを変更したい場合は、コメントを追加しないでください。代わりに「編集」をクリックし、変更を直接適用してください。 –

0

シンプルです。エンティティをインスタンス化し、オブジェクトに設定し、コントローラ内のビューに渡します。

enter image description here

エンティティ

VehicleInfoEntities DB =新しいVehicleInfoEntities()。手順

dbo.prcGetMakes()

又は

ストアド

あなたはカッコ()

dbo.prcGetMakes( "BMW")

コントローラ

public class HomeController : Controller 
{ 
    VehicleInfoEntities db = new VehicleInfoEntities(); 

    public ActionResult Index() 
    { 
     var makes = db.prcGetMakes(null); 

     return View(makes); 
    } 
} 
関連する問題