当目标是Guid时,将SqlBulkCopy与FastMember一起使用ObjectReader失败

时间:2017-08-02 21:01:31

标签: c# dapper sqlbulkcopy datareader fastmember

我有一个实体,其中有几个Guid代表表Id和外键。我正在尝试使用FastMember从其数据实体列表中创建IDataReader对象。使用Entity Framework自动生成实体。当我运行SqlBulkCopy时,我没有得到预期的插入。有时,但不是每次都会出现错误,从'System.String'到'System.Guid'的无效转换。
堆栈跟踪,似乎没有帮助,是 在System.Convert.DefaultToType(IConvertible值,Type targetType,IFormatProvider提供者)
   在System.String.System.IConvertible.ToType(Type type,IFormatProvider provider)
   在System.Convert.ChangeType(对象值,类型conversionType,IFormatProvider提供者)
   at System.Data.SqlClient.SqlParameter.CoerceValue(Object value,MetaType destinationType,Boolean& coercedToDataFeed,Boolean& typeChanged,Boolean allowStreaming)

这是实体:

[Table("HA_LineItemOptions")]
public partial class HA_LineItemOptions
{
    public System.Guid ID { get; set; }
    public System.Guid LineItemID { get; set; }
    public int ArchiveSetNumber { get; set; }
    public int WindowSetNumber { get; set; }
    public int WindowNumber { get; set; }
    public int GlassNumber { get; set; }
    public string QuestionGroup { get; set; }
    public string Question { get; set; }
    public string Answer { get; set; }
    public Nullable<System.Guid> QuestionID { get; set; }
    public Nullable<System.Guid> AnswerID { get; set; }
}

按请求编辑以包含sql表。

SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE TABLE [dbo].[HA_LineItemOptions](
    [ID] [uniqueidentifier] ROWGUIDCOL  NOT NULL,
    [LineItemID] [uniqueidentifier] NOT NULL,
    [ArchiveSetNumber] [int] NOT NULL,
    [WindowSetNumber] [int] NOT NULL,
    [WindowNumber] [int] NOT NULL,
    [GlassNumber] [int] NOT NULL,
    [QuestionGroup] [varchar](100) NULL,
    [Question] [varchar](100) NULL,
    [Answer] [varchar](8000) NULL,
    [QuestionID] [uniqueidentifier] NULL,
    [AnswerID] [uniqueidentifier] NULL,
 CONSTRAINT [PK_HA_LineItemOptions] PRIMARY KEY CLUSTERED 
(
    [ID] ASC,
    [ArchiveSetNumber] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

表中的主键是ID和ArchiveSetNumber。我在尝试读取datareader需要知道主键之后尝试添加Key Attributes,但这并没有改变行为。

这是我的SqlBulkCopy插入:

private async Task CreateBulkInsertAsync(IList<HA_LineItemOptions> lineItemOptions )
        { 
            using(var context = EntityContext.GetMwCxtFor(_programContext))
            using (var conn = new SqlConnection(context.Database.Connection.ConnectionString))
            {
              if (conn.State != ConnectionState.Open)
                    conn.Open();
                context.Database.CommandTimeout = 0;

                var trans = conn.BeginTransaction();
                try
                {
                    using (var sqlCopy = new SqlBulkCopy(conn, SqlBulkCopyOptions.Default, trans))
                    using (var reader = ObjectReader.Create(lineItemOptions))
                    {
                        var type = typeof(HA_LineItemOptions);
                        var accessor = TypeAccessor.Create(type);
                        var members = accessor.GetMembers();
                        foreach (var member in members)
                        {
                            sqlCopy.ColumnMappings.Add(new SqlBulkCopyColumnMapping(member.Name, member.Name));
                        }
                        sqlCopy.BatchSize = 10000;
                        sqlCopy.BulkCopyTimeout = 0;
                        sqlCopy.DestinationTableName = "HA_LineItemOptions";
                        await sqlCopy.WriteToServerAsync(reader);
                    }
                }
               finally
                {
                    trans.Dispose();
                    conn.Close();
                }
            }
        } 

我想在这种情况下使用SqlBulkCopy,因为我可能会插入多达100,000行。我已经切换到Dapper的InsertAsync方法,它的行为就像它应该的那样。我似乎无法在这里找到任何与Guids有关的内容,所以我感谢任何帮助/见解。

0 个答案:

没有答案