小编Gab*_*oso的帖子

与实体的交易范围

我有一个带有.NET 4的Windows窗体应用程序和用于数据层的实体框架我需要一个带有事务的方法,但是进行简单的测试我无法使其工作

在BLL中:

public int Insert(List<Estrutura> lista)
{
    using (TransactionScope scope = new TransactionScope())
    {
            id = this._dal.Insert(lista);
    }
}
Run Code Online (Sandbox Code Playgroud)

在DAL:

public int Insert(List<Estrutura> lista)
{
   using (Entities ctx = new Entities (ConnectionType.Custom))
   {
     ctx.AddToEstrutura(lista);
     ctx.SaveChanges(); //<---exception is thrown here
   }
}
Run Code Online (Sandbox Code Playgroud)

"底层提供商在Open上失败了."

有人有主意吗?

问题解决 - 我的解决方案

我解决了我的问题做了一些改变.在我的一个DAL中,我使用批量插入和其他实体.问题事务是由于事务的大部分(事务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 …
Run Code Online (Sandbox Code Playgroud)

.net c# entity

5
推荐指数
1
解决办法
3117
查看次数

在运行时获取 --launch-profile 值 .net core

我正在运行 xUnit 测试,并且设置了不同的配置文件(不同的开发人员、阶段、产品)。我能够获取 launchSettings.json 并加载配置文件设置。但因为我无法在运行时获取当前配置文件,所以我无法区分它们。

有没有一种方法可以让我在运行时获取传递给参数的配置文件值dotnet run --launch-profile <MY_PROFILE_NAME>

c# xunit .net-core

5
推荐指数
1
解决办法
742
查看次数

标签 统计

c# ×2

.net ×1

.net-core ×1

entity ×1

xunit ×1