データ層に .NET 4 と Entity Framework を使用した Windows フォーム アプリケーションがあります。
BLL で:
public int Insert(List<Estrutura> lista)
{
using (TransactionScope scope = new TransactionScope())
{
id = this._dal.Insert(lista);
}
}
DAL では:
public int Insert(List<Estrutura> lista)
{
using (Entities ctx = new Entities (ConnectionType.Custom))
{
ctx.AddToEstrutura(lista);
ctx.SaveChanges(); //<---exception is thrown here
}
}
「基になるプロバイダーが Open で失敗しました。」
誰にもアイデアはありますか?
問題が解決しました - 私の解決策
いくつかの変更を加えて問題を解決しました。私のDALの1つで、一括挿入とその他のエンティティを使用しています。問題のトランザクションは、トランザクション (トランザクション sql) の大部分がトランザクション スコープを理解していないという事実によって発生していました。そのため、DAL でエンティティを分離し、実行中の SQL トランザクションをいくつかの些細なことで使用しました。ExecuteScalar ();
これは最もエレガントな方法ではないと思いますが、トランザクションの問題は解決しました。
これが私のDALのコードです
using (SqlConnection sourceConnection = new SqlConnection(Utils.ConnectionString()))
{
sourceConnection.Open();
using (SqlTransaction transaction = sourceConnection.BeginTransaction())
{
StringBuilder query = new StringBuilder();
query.Append("INSERT INTO...");
SqlCommand command = new SqlCommand(query.ToString(), sourceConnection, transaction);
using (SqlBulkCopy bulk = new SqlBulkCopy(sourceConnection, SqlBulkCopyOptions.KeepNulls, transaction))
{
bulk.BulkCopyTimeout = int.MaxValue;
bulk.DestinationTableName = "TABLE_NAME";
bulk.WriteToServer(myDataTable);
StringBuilder updateQuery = new StringBuilder();
//another simple insert or update can be performed here
updateQuery.Append("UPDATE... ");
command.CommandText = updateQuery.ToString();
command.Parameters.Clear();
command.Parameters.AddWithValue("@SOME_PARAM", DateTime.Now);
command.ExecuteNonQuery();
transaction.Commit();
}
}
}
助けてくれてありがとう