I tend to use uniqueidentifiers for my primary key columns with a default value of newid() so the database will generate a Guid for me. I created a LinqDataSource and bound that to a DetailsView control. I was able to insert my first row within 10 minutes of writing all this code but subsequent inserts would always throw a unique constraint exception. After looking at the database, I saw my first row's Id was Guid.Empty so I knew that Linq was pushing this as a default value to the database instead of letting the database create a newid() for me. It turns out the solution is very simple. Open your DBML file, select the primary key column and change the Auto Generated Value property to true. So far, I'm still impressed with Linq.
b005caf0-36ee-42eb-abe5-874018d9566c|5|5.0
We have been experimenting with LINQ to SQL on a couple projects lately and I am constantly impressed. I'll post some tidbits as I come across some neat features.
Today I had to display the last 4 digits of a customer's credit card number on the UI. I already have a DBML file in my project and the Customer table has been added to the designer. Customer has a CreditCardNumber property (ignoring encryption for purposes of this example). Of course business logic layers aren't new but all too often, you would find logic in the UI that displays the right 4-digits. But now implementing the BLL is even easier by simply implementing a partial class.
Partial Public Class Customer
Public Readonly Property Last4 as String
Get
Return Right(Me.CreditCardNumber, 4)
End Get
End Property
End Class
You can take it one step further and override the CreditCardNumber property and change the Get to return ************XXXX instead of the clear text credit card number, which will make your business logic even more secure.
9ba3a7e9-6c1a-4247-9902-2e0a9c9d821d|3|3.3