Insight.Database - 将 Insert 与 OpenWithTransaction 结合使用
Insight.Database - using Insert with OpenWithTransaction
第一个问题:)
我是使用 Insight.Database 库的新手,但我认为我正在以正确的方式使用它。我是 运行 下面的(简化)代码,来自同一解决方案中另一个项目的 XUnit 测试。异常在 connTran.Insert 行抛出,如果我中断 CATCH 块中的日志记录功能并查看异常中的消息,它会给出错误错误 CS7069: Reference to type 'DbConnectionWrapper' claims it is defined in 'Insight.Database', but it could not be found
,但随后调试器也会在 connTran.Rollback() 行上用 A transaction has not been created for this connection
.
中断
奇怪的是,我在同一个解决方案和测试项目的另一个测试中使用了相同的代码,但使用的是平面实体并且运行良好。
我正在使用 Visual Studio 2015 企业版。调试器也没有正常运行 - 在 "inside" 交易时,将鼠标悬停在变量等上不起作用。我在 Github here 上发现了一个非常相似的 github 问题,但没有我可以使用的解决方案。
这是我正在使用的插入代码 - 我也试过 connTran.Insert 但我得到了相同的结果。
DbConnectionWrapper connTran = null;
try
{
using (connTran = _dbconnection.OpenWithTransaction())
{
var lookupHeader = connTran.QueryResults<Results>("usp_Lookup_InsertHeader", entity);
connTran.Commit();
}
}
catch(Exception ex)
{
logException(ex.Message);
connTran.Rollback();
throw;
}
实体对象如下所示:
public class LookupEntity
{
[RecordId]
public int LookupHeaderId { get; set; }
public string LookupHeaderName { get; set; }
public string LookupHeaderDescription { get; set; }
public string LookupHeaderCategory { get; set; }
public string LookupHeaderType { get; set; }
public string LookupBPMObjectName { get; set; }
public string LookupBPMMethodName { get; set; }
public string LookupBPMInputParams { get; set; }
public string LookupBPMExtraParams { get; set; }
public string LookupBPMOutputDataSetName { get; set; }
public string LookupBPMOutputNameNode { get; set; }
public string LookupBPMOutputValueNode { get; set; }
public string LookupBPMOutputActiveNode { get; set; }
public int Active { get; set; }
public int Cache { get; set; }
public int CsysLastUpdateBy { get; set; }
public DateTime? CsysLastUpdateDate { get; set; }
public int CsysInsertBy { get; set; }
public DateTime? CsysInsertDate { get; set; }
public string CsysTimeStamp { get; set; }
public string CsysTag { get; set; }
public int CsysOwnerId { get; set; }
public string CsysOwnerType { get; set; }
public int CsysRecordStatus { get; set; }
[ChildRecords]
public List<LookupDetail> LookupDetails { get; set; }
}
好吧......更多的是在 Insight 源代码中乱搞和研究,我解决了我原来的问题,并遇到了更多。我将 post 我的发现放在这里,以防 Insight 作者看到。
我最初问题的答案是将我的 TRY..CATCH 重组为在 USING 中 - 这可能很明显,也许不是,但现在我知道了:)所以我的代码变成了这个:
using (var connTran = _dbconnection.OpenWithTransaction())
{
try
{
connTran.Insert("usp_Lookup_InsertHeader", entity, new { lookupHeader = entity });
connTran.Commit();
}
catch(Exception ex)
{
logException(ex.Message);
connTran.Rollback();
throw;
}
}
请注意,我也可以避免在使用之外声明 connTran 变量。
自从我使用 SQL 2008 以来,我很想在插入存储过程中使用 Table 值参数。这给了我下一个令人头疼的问题 - 这可能与过时的文档有关。
doco 指出代码如下:
connTran.Insert("usp_Lookup_InsertHeader", entity);
将插入记录,然后将新的身份值映射回实体,假设返回的 id 字段和实体 属性 名称匹配(他们这样做)。 Insert 存储过程具有如下签名:
CREATE PROCEDURE [dbo].[usp_Lookup_InsertHeader]
@lookupHeader [lookupHeaderType] READONLY
Insight 一直抱怨 "lookupHeader" 参数没有定义,所以我最终在 doco 的其他地方偶然发现了一些东西,把我的代码变成了这个:
connTran.Insert("usp_Lookup_InsertHeader", entity, new { lookupHeader = entity });
现在 Insight 很高兴 :)
第三期就变成了日期时间值。
实体中的CsysLastUpdateDate
属性被定义为DateTime?
。在 TVP 的 SQL 类型中,CsysLastUpdateDate
字段被定义为 DateTime
。在我的测试中,我将 CsysLastUpdateDate
设置为 DateTime.Now
观察 SQL Profiler,我发现 SQL 文本 Insight posting 将毫秒包含在现在日期时间值的字符串表示形式中。下面是此文本的示例,其中包含字符串日期时间 - '2016-06-16 18:03:32.5510000'.
declare @p1 dbo.lookupHeaderType
insert into @p1 values(0,N'timbo.test',N'',N'',N'',N'',N'',N'',N'',N'',N'',N'',N'',1,1,2,'2016-06-16 18:03:32.5510000',2,'2016-06-16 18:03:32.5510000',N'',N'',1,N'cSysSite',1)
当 SQL 尝试执行该文本以创建 TVP 时,它因日期时间转换问题而出错。如果我手动编辑日期时间字符串中的毫秒数并执行文本,则 TVP 会正确创建。
随着更多的尝试,我发现将 Type 中的 CsysLastUpdateDate
字段声明为 DateTime2
字段,Insight 发送的 SQL 执行成功并且 Insert 工作正常.
我不确定我是否发现了错误或者这些只是新手学习,但我希望这对下一个人有所帮助:)
第一个问题:)
我是使用 Insight.Database 库的新手,但我认为我正在以正确的方式使用它。我是 运行 下面的(简化)代码,来自同一解决方案中另一个项目的 XUnit 测试。异常在 connTran.Insert 行抛出,如果我中断 CATCH 块中的日志记录功能并查看异常中的消息,它会给出错误错误 CS7069: Reference to type 'DbConnectionWrapper' claims it is defined in 'Insight.Database', but it could not be found
,但随后调试器也会在 connTran.Rollback() 行上用 A transaction has not been created for this connection
.
奇怪的是,我在同一个解决方案和测试项目的另一个测试中使用了相同的代码,但使用的是平面实体并且运行良好。
我正在使用 Visual Studio 2015 企业版。调试器也没有正常运行 - 在 "inside" 交易时,将鼠标悬停在变量等上不起作用。我在 Github here 上发现了一个非常相似的 github 问题,但没有我可以使用的解决方案。
这是我正在使用的插入代码 - 我也试过 connTran.Insert 但我得到了相同的结果。
DbConnectionWrapper connTran = null;
try
{
using (connTran = _dbconnection.OpenWithTransaction())
{
var lookupHeader = connTran.QueryResults<Results>("usp_Lookup_InsertHeader", entity);
connTran.Commit();
}
}
catch(Exception ex)
{
logException(ex.Message);
connTran.Rollback();
throw;
}
实体对象如下所示:
public class LookupEntity
{
[RecordId]
public int LookupHeaderId { get; set; }
public string LookupHeaderName { get; set; }
public string LookupHeaderDescription { get; set; }
public string LookupHeaderCategory { get; set; }
public string LookupHeaderType { get; set; }
public string LookupBPMObjectName { get; set; }
public string LookupBPMMethodName { get; set; }
public string LookupBPMInputParams { get; set; }
public string LookupBPMExtraParams { get; set; }
public string LookupBPMOutputDataSetName { get; set; }
public string LookupBPMOutputNameNode { get; set; }
public string LookupBPMOutputValueNode { get; set; }
public string LookupBPMOutputActiveNode { get; set; }
public int Active { get; set; }
public int Cache { get; set; }
public int CsysLastUpdateBy { get; set; }
public DateTime? CsysLastUpdateDate { get; set; }
public int CsysInsertBy { get; set; }
public DateTime? CsysInsertDate { get; set; }
public string CsysTimeStamp { get; set; }
public string CsysTag { get; set; }
public int CsysOwnerId { get; set; }
public string CsysOwnerType { get; set; }
public int CsysRecordStatus { get; set; }
[ChildRecords]
public List<LookupDetail> LookupDetails { get; set; }
}
好吧......更多的是在 Insight 源代码中乱搞和研究,我解决了我原来的问题,并遇到了更多。我将 post 我的发现放在这里,以防 Insight 作者看到。
我最初问题的答案是将我的 TRY..CATCH 重组为在 USING 中 - 这可能很明显,也许不是,但现在我知道了:)所以我的代码变成了这个:
using (var connTran = _dbconnection.OpenWithTransaction())
{
try
{
connTran.Insert("usp_Lookup_InsertHeader", entity, new { lookupHeader = entity });
connTran.Commit();
}
catch(Exception ex)
{
logException(ex.Message);
connTran.Rollback();
throw;
}
}
请注意,我也可以避免在使用之外声明 connTran 变量。
自从我使用 SQL 2008 以来,我很想在插入存储过程中使用 Table 值参数。这给了我下一个令人头疼的问题 - 这可能与过时的文档有关。
doco 指出代码如下:
connTran.Insert("usp_Lookup_InsertHeader", entity);
将插入记录,然后将新的身份值映射回实体,假设返回的 id 字段和实体 属性 名称匹配(他们这样做)。 Insert 存储过程具有如下签名:
CREATE PROCEDURE [dbo].[usp_Lookup_InsertHeader]
@lookupHeader [lookupHeaderType] READONLY
Insight 一直抱怨 "lookupHeader" 参数没有定义,所以我最终在 doco 的其他地方偶然发现了一些东西,把我的代码变成了这个:
connTran.Insert("usp_Lookup_InsertHeader", entity, new { lookupHeader = entity });
现在 Insight 很高兴 :)
第三期就变成了日期时间值。
实体中的CsysLastUpdateDate
属性被定义为DateTime?
。在 TVP 的 SQL 类型中,CsysLastUpdateDate
字段被定义为 DateTime
。在我的测试中,我将 CsysLastUpdateDate
设置为 DateTime.Now
观察 SQL Profiler,我发现 SQL 文本 Insight posting 将毫秒包含在现在日期时间值的字符串表示形式中。下面是此文本的示例,其中包含字符串日期时间 - '2016-06-16 18:03:32.5510000'.
declare @p1 dbo.lookupHeaderType
insert into @p1 values(0,N'timbo.test',N'',N'',N'',N'',N'',N'',N'',N'',N'',N'',N'',1,1,2,'2016-06-16 18:03:32.5510000',2,'2016-06-16 18:03:32.5510000',N'',N'',1,N'cSysSite',1)
当 SQL 尝试执行该文本以创建 TVP 时,它因日期时间转换问题而出错。如果我手动编辑日期时间字符串中的毫秒数并执行文本,则 TVP 会正确创建。
随着更多的尝试,我发现将 Type 中的 CsysLastUpdateDate
字段声明为 DateTime2
字段,Insight 发送的 SQL 执行成功并且 Insert 工作正常.
我不确定我是否发现了错误或者这些只是新手学习,但我希望这对下一个人有所帮助:)