EF4「Code First」を使用して、テーブルを返す 1 つのパラメーターでストアド プロシージャを実行したいと考えています。この目的のためだけにいくつかの DTO を使用しても問題ありません。エンティティを返す必要はありません。私はしようとしました:
a) 関数インポートを使用して edmx ファイルを作成し、次のように ObjectContext に追加します。
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.RegisterEdmx("Model.edmx");
}
しかし、InvalidOperationException
「流暢なAPIを使用したコードの最初の構成が開始された後、既存のモデルの登録は使用できません」と言われてしまいました。
b) アンダーリング接続にアクセスし、手順を実行します。
var connection = this.objectContext.UnderlyingContext.Connection;
connection.Open();
DbCommand command = connection.CreateCommand();
command.CommandType = CommandType.StoredProcedure;
command.CommandText = "booklog.Recommendations";
command.Parameters.Add(
new EntityParameter("userId", DbType.Guid) { Value = this.userId });
var reader = command.ExecuteReader();
// etc.
どこ
public class MyObjectContext : DbContext
{
public System.Data.Objects.ObjectContext UnderlyingContext
{
get { return this.ObjectContext; }
}
// ....
}
しかし、このアプローチもうまくいきません。InvalidOperationException
「FunctionImport に指定されたコンテナ 'booklog' が現在のワークスペースに見つかりませんでした」というメッセージがスローされます。