Composite keys marked as read-only?

Aug 25, 2010 at 12:43 AM

I have several tables with composite primary keys, and for some reason the generated code produces properties for the columns that are read-only. How can I define the model so that I can set the values for these columns in the code?

Aug 25, 2010 at 1:02 AM

All keys are read-only. In NHDAL this is still true. With the newest version of the EFDAL (posted tonight) this is no longer true. If you are using the NHDAL (traditional non-EF) you can set the key when you create the object. For example if you have a customer collection you would set the CustomerID like this. The keys are read-only because when someone loads an objects the key defines the actual object in the database so you do not want to accidental set it and mess up your insert scripts.

CustomerCollection customerCollection = new CustomerCollection();
Customer customer = customerCollection.NewItem(47); //Set PK here
 customer.Name = "John Doe";
customerCollection.AddItem( customer); 

Aug 25, 2010 at 12:37 PM

Ah, set the keys in the NewItem method. I figured I was missing something simple. Thank you for the help.