Tuesday, February 19, 2008

How to autogenerate PK - Guid and other default properties in your LINQ class

partial class Invitation
{
partial void OnCreated()
{
if (Id == Guid.Empty)
{
this.Id = Guid.NewGuid();
this.CreatedDate = DateTime.Now;
}
}

}
Also in this sample I am setting CreatedDate for my objects. Works fine.




Applies to all dotnet languages: C#, VB.NET, C++.NET, J#

Labels:

LINQ - ChangeConflictException when updating row

System.Data.Linq.ChangeConflictException: Row not found or changed


I had very simple 4 lines:

ltsRmzLightDataContext data = new ltsRmzLightDataContext();

invitation = data.Invitations.SingleOrDefault(email => email.Id == invitationId);

if (invitation != null)

{

invitation.IsAccepted = true;

data.SubmitChanges();

}

and was getting System.Data.Linq.ChangeConflictException: Row not found or changed in this simple piece.

After long googling I fed up and reviewed my class in designer. For me, my problem was that I added few properties and one of them has incorrect name and NUllable property set.

After fixing this it all started to work fine.



Applies to all dotnet languages: C#, VB.NET, C++.NET, J#

Labels: