This project has moved and is read-only. For the latest updates, please go here.

Failed to convert parameter value from a Int32 to a Guid.

Jul 14, 2015 at 6:41 PM
I'm getting the following error:

[System.InvalidOperationException] = {"The given value of type Int32 from the data source cannot be converted to type uniqueidentifier of the specified target column."}

I've tried to specify SqlBulkCopyOptions.KeepIdentity, but I'm still running into the problem. I'm at a dead-end of where to go next.

Ideas? I can provide whatever information needs to help resolve this issue!

Thanks very much in advance!

-Jason
Oct 26, 2015 at 9:20 AM
I've got the same problem. I had two classes:
    public class Session
    {
        pubilc Guid Id { get; set; }
        public string Page { get; set; }
    }
    public class SessionParam
    {
        pubilc Guid Id { get; set; }
        public Session Session { get; set; }
        public string Value { get; set; }
    }
My function for saving was:
        protected override void BulkInsertData(
            BulkBatchInsert[] batchInserts, BulkInsertOptions opts)
        {
            var sessions = batchInserts.SelectMany(bi => bi.Sessions).ToList();
            foreach(var session in sessions)
            {
                session.Id = Guid.NewGuid();
                foreach (var p in session.Params)
                {
                    p.Id = Guid.NewGuid();
                    p.Session = session;
                }
            }
            var seesionParams = sessions.SelectMany(s => s.Params).ToList();
            _ctx.BulkInsert(sessions, opts);
            _ctx.BulkInsert(seesionParams, opts); // <- Exception
        }
I solved my problem by adding SessionId and initing it.
    public class SessionParam
    {
        pubilc Guid Id { get; set; }
        public Guid SessionId { get; set; }
        public Session Session { get; set; }
        public string Value { get; set; }
    }
        protected override void BulkInsertData(
            BulkBatchInsert[] batchInserts, BulkInsertOptions opts)
        {
            var sessions = batchInserts.SelectMany(bi => bi.Sessions).ToList();
            foreach(var session in sessions)
            {
                session.Id = Guid.NewGuid();
                foreach (var p in session.Params)
                {
                    p.Id = Guid.NewGuid();
                    p.SessionId = session.Id;
                }
            }
            var seesionParams = sessions.SelectMany(s => s.Params).ToList();
            _ctx.BulkInsert(sessions, opts);
            _ctx.BulkInsert(seesionParams, opts);
        }