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
|
Hi,
I was wondering if there was a way to update an entity without loading it first? I
just discovered how to do this with Linq to SQL and was hoping to replicate that with LightSpeed.
Thanks!
James
|
|
|
Yes, you can do this using the IUnitOfWork.Update(Query, object) method. Pass a Query to specify which entities you want to update. The object can either be a dictionary, in which case it is interpreted as property-name/value pairs, or a normal object, in which case the property values are applied. Note that you must specify the entity type in the Query because this is used to map property names to column names; however, if you pass an object, it doesn't have to be an instance of that entity type, just have compatible names. Examples: Query query = new Query { EntityType = typeof(Person), QueryExpression = Entity.Attribute("Id") == 123 }; // Each of the following sets Person 123's FirstName to "Bob" // using a dictionary // using an anonymous type // The following sets *all* of Person 123's properties -- if you only mean to Hope this makes sense -- let me know if you need clarification. |
|
|
Great! What do you think might happen if I pass in an interface that only contained a subset of the Person attributes, but the underlying object was actually a Person? That way I wouldn't need to define a new object to do a partial update, but just a subset of fields in an interface..
|
|
|
It would pick up all of the properties from the Person, not just those in the interface. The reason is that the Update method can't know the declared type of the variable in your code: we only know the *runtime* type of the object we receive as the argument. Sorry. Your best bet is to use an anonymous type. This is actually reasonably concise because the compiler will automatically synthesise property names, e.g. new { person.FirstName, person.LastName }, so although it's not quite as convenient as an interface it shouldn't be too painful either! |
|