title: "Member AutoSync Failure" using TPT, view and instead of insert trigger
body: <p>Trying to use Linq2SQL with class inheritance using TPT pattern like this http://blogs.msdn.com/sbajaj/archive/2008/04/02/tpt-with-linq-to-sql.aspx</p>
  
  <p>Sample application works fine. Here is my code:</p>
  
  <p>Tables and views:</p>
  
  <p>&nbsp;</p>
  
  
  <pre>CREATE TABLE [dbo].[social_event](
      [Id] [int] IDENTITY(1,1) NOT NULL PRIMARY KEY,
      [Creator] [uniqueidentifier] NOT NULL FOREIGN KEY REFERENCES [dbo].[aspnet_Users](UserId),
      [EventDate] [datetime] NOT NULL)
  GO
  
  CREATE TABLE [dbo].[social_addfriendevent](
      [ID] [int] NOT NULL PRIMARY KEY FOREIGN KEY REFERENCES [dbo].[social_event](Id),
      [Friend] [uniqueidentifier] NOT NULL FOREIGN KEY REFERENCES [dbo].[aspnet_Users](UserId),
      [Added] [bit] NOT NULL DEFAULT((0)))
  GO
  
  CREATE VIEW [dbo].[event]
  AS
  SELECT t0.Id, t0.Creator, t0.EventDate, t1.Friend, t1.Added, 'AddFriendEvent' AS EventType
  FROM  dbo.social_event AS t0 INNER JOIN
     dbo.social_addfriendevent AS t1 ON t0.Id = t1.Id
  GO
  </pre>
  
  
  <p>&nbsp;</p>
  
  <p>&nbsp;My "Instead of insert" trigger:</p>
  
  <p>&nbsp;</p>
  
  
  <pre>CREATE TRIGGER [dbo].[trigger_update_event] ON [dbo].[event] INSTEAD OF INSERT
  AS
      DECLARE @rc AS INT;
      SET @rc = @@ROWCOUNT;
      IF @rc = 0 RETURN;
  
      DECLARE @Id AS INT
      DECLARE @EventType AS NVARCHAR(50)
      DECLARE @Creator AS uniqueidentifier
      DECLARE @EventDate AS datetime
      DECLARE @Friend AS uniqueidentifier
      DECLARE @Added AS bit 
  
      IF @rc = 1
          BEGIN
              SELECT @Id=Id,
     @EventType=EventType,
                   @Creator=Creator,
                   @EventDate = getdate(),
                   @Friend=Friend,
                   @Added=Added
              FROM INSERTED
  
              INSERT INTO dbo.social_event VALUES(@Creator, @EventDate)
              SELECT @Id = SCOPE_IDENTITY()
              IF @EventType = 'AddFriendEvent'
                  INSERT INTO dbo.social_addfriendevent VALUES(@Id, @Friend, @Added)                            
          END
      ELSE
          BEGIN
              DECLARE cursor_inserted CURSOR FAST_FORWARD FOR
                  SELECT EventType,
                       Creator,
                       EventDate,
                       Friend,
                       Added
                  FROM INSERTED
              OPEN cursor_inserted;
  
              FETCH NEXT FROM cursor_inserted INTO @EventType,
                                               @Creator,
                                               @EventDate,
                                               @Friend,
                                               @Added;
              WHILE @@FETCH_STATUS = 0
              BEGIN
                  INSERT INTO dbo.social_event VALUES(@Creator, @EventDate)
                  SELECT @Id = SCOPE_IDENTITY()                
                  IF @EventType = 'AddFriendEvent'
                  INSERT INTO dbo.social_addfriendevent VALUES(@Id, @Friend, @Added)
              END
              CLOSE cursor_inserted;
              DEALLOCATE cursor_inserted;
          END
  </pre>
  
  
  <p>&nbsp;</p>
  
  <p>My data classes with mappings:</p>
  
  <p>&nbsp;</p>
  
  
  <pre>[InheritanceMapping(Code = "AddFriendEvent", Type = typeof(AddFriendEvent), IsDefault = true)]
   [Table(Name = "dbo.event")]
   public abstract class Event
   {
   private static int s_id;
  
   public Event()
   {
    this.Id = --Event.s_id;
   }
  
   public Event(string eventType)
    : this()
   {
    EventType = eventType;
   }
  
   [Column(IsPrimaryKey = true)]
   public int Id;
  
   [Column(Name = "Creator")]
   public Guid CreatorId;
  
   [Column(Name = "EventDate", DbType = "datetime")] 
   public DateTime Date;
  
   [Column(IsDiscriminator = true, Name = "EventType")]
   public string EventType;
   }
  
   public class AddFriendEvent : Event
   {
   [Column(Name = "Added", CanBeNull = false)]
   public bool Added;
  
   [Column(Name = "Friend", CanBeNull = false)]
   public Guid Friend;
  
   public AddFriendEvent()
    : base("AddFriendEvent")
   {
   }
   }
  </pre>
  
  
  <p>&nbsp;</p>
  
  <p>When I try to create new "AddFriendEvent" and save it to database like here:</p>
  
  <p>&nbsp;</p>
  
  <p>&nbsp;</p>
  
  
  <pre>DataContext dc = new DataContext(connectionString);
     Table&lt;Event&gt; allEvents = dc.GetTable&lt;Event&gt;();
     AddFriendEvent ev = new AddFriendEvent()
     {
      Added = true,
      CreatorId = Guid.Parse("379959D-58FE-4A57-BB02-559E3E855B8F"),
      Date = DateTime.Now,
      Friend = Guid.Parse("379959D-58FE-4A57-BB02-559E3E855B8F")
     };
     allEvents.InsertOnSubmit(ev);
     dc.SubmitChanges();
  </pre>
  
  
  <p>&nbsp;</p>
  
  <p>I got&nbsp;<strong>"Member AutoSync Failure"</strong>. What's wrong?</p>
  
  <p><br /></p>