FirstName = "Scott";
con.MiddleName = "L";
con.LastName = "Klein";
con.Title = "Geek";
con.EmailAddress = "geek@email.com";
con.EmailPromotion = 1;
con.NameStyle = 0;
con.PasswordHash = "";
con.PasswordSalt = "";
db.Contact.Add(con);
db.SubmitChanges();
textBox1.Text = "Contact created.";
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
223
Part III: LINQ to SQL
You are ready to test it. Run the application and click the Insert button. Didn??™t work, did it? The error
you should have received states that the table is read-only. This error is misleading (you??™ll see why in a
minute), but the fix is simple. On the definition of the ContactID column, two ContactID properties need
to be set, as shown here:
[Table(Name = "Person.Contact")]
public class Contact
{
[Column(DBType = "int not null", IsPrimaryKey=true, IsDBGenerated=true)]
public int ContactID;
Now when you run the application and click the Insert button, the ??????Contact created??™??™ message appears
in the text box.
Why did the first insert fail? LINQ to SQL treats tables as read-only by default, and it needs to know
which column is the primary key.
Pages:
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366