- InvalidCastException on LINQ-2-SQL Model When Inserting
- CREATE TABLE [dbo].[Timer](
- [TimerId] [int] IDENTITY(1,1) NOT NULL,
- [UserName] [nvarchar](64) NOT NULL,
- [TimeStamp] [datetime] NOT NULL,
- [SessionId] [uniqueidentifier] NOT NULL,
- [Action] [nvarchar](50) NOT NULL,
- [ActionId] [uniqueidentifier] NOT NULL,
- [Description] [nvarchar](256) NOT NULL,
- CONSTRAINT [PK_Timer] PRIMARY KEY CLUSTERED
- (
- [TimerId] ASC
- )
- public partial class Timer
- {
- [Column(AutoSync=AutoSync.OnInsert, DbType="Int NOT NULL IDENTITY", IsPrimaryKey=true, IsDbGenerated=true)]
- public int TimerId { get; set; }
- [Column(DbType = "NVarChar(64) NOT NULL", CanBeNull = false)]
- public string UserName { get; set; }
- [Column(DbType = "DateTime NOT NULL")]
- public DateTime TimeStamp { get; set; }
- [Column(DbType = "UniqueIdentifier NOT NULL")]
- public Guid SessionId { get; set; }
- [Column(DbType = "NVarChar(50) NOT NULL", CanBeNull = false)]
- public string Action { get; set; }
- [Column(DbType = "UniqueIdentifier NOT NULL")]
- public Guid ActionId { get; set; }
- [Column(DbType = "NVarChar(256) NOT NULL", CanBeNull = false)]
- public string Description { get; set; }
- }
- using (var ctx = new MyDataContext())
- {
- var timer = new Timer();
- timer.Action = "Start";
- timer.ActionId = Guid.NewGuid();
- timer.Description = "foo";
- timer.SessionId = Guid.NewGuid();
- timer.TimeStamp = DateTime.UtcNow;
- timer.UserName = "foo";
- ctx.Timers.InsertOnSubmit(timer);
- ctx.SubmitChanges(); // exception thrown here
- }