Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jun 30th, 2012  |  syntax: None  |  size: 1.56 KB  |  hits: 12  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. InvalidCastException on LINQ-2-SQL Model When Inserting
  2. CREATE TABLE [dbo].[Timer](
  3.     [TimerId] [int] IDENTITY(1,1) NOT NULL,
  4.     [UserName] [nvarchar](64) NOT NULL,
  5.     [TimeStamp] [datetime] NOT NULL,
  6.     [SessionId] [uniqueidentifier] NOT NULL,
  7.     [Action] [nvarchar](50) NOT NULL,
  8.     [ActionId] [uniqueidentifier] NOT NULL,
  9.     [Description] [nvarchar](256) NOT NULL,
  10.  CONSTRAINT [PK_Timer] PRIMARY KEY CLUSTERED
  11. (
  12.     [TimerId] ASC
  13. )
  14.        
  15. public partial class Timer
  16. {
  17.  
  18.     [Column(AutoSync=AutoSync.OnInsert, DbType="Int NOT NULL IDENTITY", IsPrimaryKey=true, IsDbGenerated=true)]
  19.     public int TimerId { get; set; }
  20.  
  21.     [Column(DbType = "NVarChar(64) NOT NULL", CanBeNull = false)]
  22.     public string UserName { get; set; }
  23.  
  24.     [Column(DbType = "DateTime NOT NULL")]
  25.     public DateTime TimeStamp { get; set; }
  26.  
  27.     [Column(DbType = "UniqueIdentifier NOT NULL")]
  28.     public Guid SessionId { get; set; }
  29.  
  30.     [Column(DbType = "NVarChar(50) NOT NULL", CanBeNull = false)]
  31.     public string Action { get; set; }
  32.  
  33.     [Column(DbType = "UniqueIdentifier NOT NULL")]
  34.     public Guid ActionId { get; set; }
  35.  
  36.     [Column(DbType = "NVarChar(256) NOT NULL", CanBeNull = false)]
  37.     public string Description { get; set; }
  38.  
  39. }
  40.        
  41. using (var ctx = new MyDataContext())
  42. {
  43.     var timer = new Timer();
  44.     timer.Action = "Start";
  45.     timer.ActionId = Guid.NewGuid();
  46.     timer.Description = "foo";
  47.     timer.SessionId = Guid.NewGuid();
  48.     timer.TimeStamp = DateTime.UtcNow;
  49.     timer.UserName = "foo";
  50.  
  51.     ctx.Timers.InsertOnSubmit(timer);
  52.     ctx.SubmitChanges();  // exception thrown here
  53. }