为什么我的DBML在InsertOnSbumit上将varchar(8000)发送到数据库?

时间:2018-07-11 01:03:36

标签: c# sql-server-2008 dbml

我的dbml中有一个非常简单的表,其中有一些varchar(20)和2 varchar(50)列:

Simple Table

这是我的dbml的xml定义:

        <?xml version="1.0" encoding="utf-8"?>
<Database Name="AppraisalsLenderX" Class="AppraisalsLenderXDataContext" xmlns="http://schemas.microsoft.com/linqtosql/dbml/2007">
      <Connection Mode="AppSettings" ConnectionString="Data Source=emdb.gr-stage.com;Initial Catalog=AppraisalsLenderX;User ID=LenderXSQLUser" SettingsObjectName="GuaranteedRate.Appraisal.Infrastructure.Data.Properties.Settings" SettingsPropertyName="AppraisalsLenderXConnectionString" Provider="System.Data.SqlClient" />
      <Table Name="dbo.LenderXMessage" Member="LenderXMessages">
        <Type Name="LenderXMessage">
          <Column Name="LenderXMessageID" Type="System.Int32" DbType="Int NOT NULL IDENTITY" IsPrimaryKey="true" IsDbGenerated="true" CanBeNull="false" />
          <Column Name="EventType" Type="System.String" DbType="VarChar(50) NOT NULL" CanBeNull="false" />
          <Column Name="EventID" Type="System.String" DbType="VarChar(50) NOT NULL" CanBeNull="false" />
          <Column Name="AppraisalOrderID" Type="System.String" DbType="VarChar(20) NOT NULL" CanBeNull="false" />
          <Column Name="LoanNumber" Type="System.String" DbType="VarChar(20) NOT NULL" CanBeNull="false" />
          <Column Name="OrderStatus" Type="System.String" DbType="VarChar(20) NOT NULL" CanBeNull="false" />
          <Column Name="LoanOfficerEmailAddress" Type="System.String" DbType="VarChar(20)" CanBeNull="true" />
        </Type>
      </Table>
    </Database>

这是我使用实体的方式:

            using (AppraisalsLenderXDataContext db = new AppraisalsLenderXDataContext(ConfigurationManager.ConnectionStrings["AppraisalsLenderX"].ConnectionString))
        {
            LenderXMessage message = new LenderXMessage();
            message.EventType = lxEvent.EventType;
            message.EventID = lxEvent.EventID;
            message.AppraisalOrderID = lxEvent.LXData.AppraisalOrder.AppraisalOrderID;
            message.LoanNumber = lxEvent.LXData.AppraisalOrder.LXAppFile.LoanNumber;
            message.OrderStatus = lxEvent.LXData.AppraisalOrder.OrderStatus;
            message.LoanOfficerEmailAddress = lxEvent.LXData.AppraisalOrder.LoanOfficerInfo.LoanOfficerAccount;
            db.LenderXMessages.InsertOnSubmit(message);
            try
            {
                db.SubmitChanges();
            }
            catch (Exception ex)
            {
                log.Error($"Error attempting to insert a record into AppraisalsLenderX database ... StackTrace: {ex.ToString()}");
            }
        }
    }

问题:为什么在我SubmitChanges()时将其发送到SQL引擎:

    exec sp_executesql N'INSERT INTO [dbo].[LenderXMessage]([LenderXMessageID], [EventType], [EventID], [AppraisalOrderID], [LoanNumber], [OrderStatus], [LoanOfficerEmailAddress])
VALUES (@p0, @p1, @p2, @p3, @p4, @p5, @p6)',N'@p0 uniqueidentifier,@p1 varchar(8000),@p2 varchar(8000),@p3 varchar(8000),@p4 varchar(8000),@p5 varchar(8000),@p6 varchar(8000)',@p0='00000000-0000-0000-0000-000000000000',@p1='Event.Appraisal.Request.DatesSet',@p2='311AED46-7689-11E8-B8A0-2CEE6AEAE87B',@p3='248422',@p4='161481439',@p5='in_progress',@p6='testloanofficer@aaa.com'

是否注意到varchar(8000)? 我当然会收到此错误:

System.Data.SqlClient.SqlException (0x80131904): String or binary data would be truncated.

为什么?

1 个答案:

答案 0 :(得分:-1)

我必须修剪来自xml规范的字符串:

[XmlRoot("event")]
public class LenderXEvent
{
    [XmlElement("type")]
    public string EventType { get; set; }
    [XmlElement("id")]
    public string EventID { get; set; }
    [XmlElement("data")]
    public LenderXData LXData { get; set; }
}

我正在发送到数据库。现在可以使用了。这是更新的代码:

    using (AppraisalsLenderXDataContext db = new AppraisalsLenderXDataContext(ConfigurationManager.ConnectionStrings["AppraisalsLenderX"].ConnectionString))
{
    LenderXMessage message = new LenderXMessage();
    message.EventType = lxEvent.EventType.Substring(0, lxEvent.EventType.Length);
    message.EventID = lxEvent.EventID.Substring(0, lxEvent.EventID.Length);
    message.AppraisalOrderID = lxEvent.LXData.AppraisalOrder.AppraisalOrderID.Substring(0, lxEvent.LXData.AppraisalOrder.AppraisalOrderID.Length);
    message.LoanNumber = lxEvent.LXData.AppraisalOrder.LXAppFile.LoanNumber.Substring(0, lxEvent.LXData.AppraisalOrder.LXAppFile.LoanNumber.Length);
    message.OrderStatus = lxEvent.LXData.AppraisalOrder.OrderStatus.Substring(0, lxEvent.LXData.AppraisalOrder.OrderStatus.Length);
    message.LoanOfficerEmailAddress = lxEvent.LXData.AppraisalOrder.LoanOfficerInfo.LoanOfficerAccount.Substring(0, lxEvent.LXData.AppraisalOrder.LoanOfficerInfo.LoanOfficerAccount.Length);
    message.DateCreated = DateTime.Now;
    db.LenderXMessages.InsertOnSubmit(message);
    try
    {
        db.SubmitChanges();
    }
    catch (Exception ex)
    {
        log.Error($"Error attempting to insert a record into AppraisalsLenderX database ... StackTrace: {ex.ToString()}");
    }
}

感谢@mjwillis的答案。