This thread looks to be a little on the old side and therefore may no longer be relevant. Please see if there is a newer thread on the subject and ensure you're using the most recent build of any software if your question regards a particular product.
This thread has been locked and is no longer accepting new posts, if you have a question regarding this topic please email us at support@mindscape.co.nz
|
Hello,
here is a simple example:
{
Customer customer = new Customer();
uow.Attach(customer);
Order order = new Order();
customer.Orders.Add(order);
customer.Orders.Clear();
uow.SaveChanges();
}
If I use for Customer Entity Identity Type = int, it works.
But if I change it to string, I get an exception "Cannot insert the value NULL into column 'CustomerId', table 'testDB.dbo.Order1'; column does not allow nulls. INSERT fails. The statement has been terminated."
Do you have any ideas why it happens?
Thank you!
|
|
|
LightSpeed doesn't provide an automatic ID generator for string identities, only for numeric and GUID identities. (The default ID generator is KeyTable, which is for numeric identities.) String IDs are really intended only for use with existing databases that use a natural key. For information about implementing natural keys, see http://www.mindscapehq.com/documentation/lightspeed/Working-with-Legacy-Databases/Using-Natural-Keys If you're not constrained by an existing database, we'd strongly encourage you to use surrogate keys rather than natural keys -- you can then use numeric or GUID keys and take advantage of the appropriate built-in identity methods (see http://www.mindscapehq.com/documentation/lightspeed/Controlling-the-Database-Mapping/Identity-Generation). |
|
|
Yeah, but if I use a natural key in this situation and if its type is String, it doesn't work either and I get the same exception. |
|
|
Hi, Ivan! I`d like to add some extra information to Question`s post. It doesn`t matter why we use String Identity - we really need to use it. When we try to clear Customer`s (Customer has a string identity) Orders collection, LightSpeed doesn`t delete Orders from database. It just updates foreign keys of Orders (it tries to set it to NULL). At the same time, if we use numeric identity of Customer and try to clear Orders collection, LightSpeed will delete Orders from database perfectly! Is it a bug? Thanks! |
|
|
Dmitriy, It's a limitation. Since the CLR String type is nullable, LightSpeed thinks the association is not dependent. To override this, make the association dependent explicitly (in the designer, select the association arrow and set Is Dependent to True). Question, It sounds like the natural key is not being set up. Are you overriding GeneratedId() as discussed in the documentation? If you are overriding GeneratedId() and you are still getting the 'cannot insert NULL' error, can you provide us with a small, buildable console application or NUnit test suite (containing only a single entity class -- we don't need to see the whole model) that reproduces the problem? Thanks! |
|
|
Hi, Ivan! We - Question and I - have told you about the same problem. Actually, we override Entity`s GenerateId() to be able to set entity`s Id from the outside: [Serializable] But I think, when LS perform the removing of the child collection, it doesn`t care about HOW Id was generated. Isn`t it? Any way, if I even set Is Dependent to true, LS still tries to UPDATE the foreign keys! I have attached picture with the schema. Here is a code sample: Customer customer; Can you say, why Is Dependent doesn`t work? |
|
|
I'm not sure why this isn't working for you. Can you post a stripped-down project (console application or NUnit test, just the two entities involved) that exhibits the problem please? You can attach a zip file via the Options tab (please remove all binaries first). Thanks! |
|
|
Ok, here is the project |
|
|
Hello Dmitriy and Question, Thanks for the repro case. It seems we had a bug where Is Dependent was not being respected if the key was a reference type such as string. There will be a fix for this in the next nightly build (available from about 1200 UTC). Thanks for drawing our attention to this -- let us know if you continue to see problems! |
|
|
Hi, Ivan! It works perfect now! Thanks! |
|