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, I come from doing Entity Framework. What is the LightSpeed version of this piece of code?
if (ModelState.IsValid) { db.Entry(movie).State = EntityState.Modified; db.SaveChanges(); return RedirectToAction("Index"); } return View(movie);
Well, there is the way of removing then adding but I think there's should be an equivalent way of the above. This is what I came up with so far (and it couldn't work, of course which is why I'm here):
bool isEdited = false; using (var uow = context.CreateUnitOfWork()) { uow.Businesses.Single(p => p.Id == business.Id) = business; uow.SaveChanges(); isEdited = true; }
if (isEdited) { return RedirectToAction("Index"); } return View(business); |
|
|
LightSpeed automatically sets the EntityState to Modified when you make a change to the entity. You don't need to force the state to Modified manually. Thus: var business = uow.Businesses.Single(b => b.Id == id); |
|
|
Oh, I saw that in the tutorials but I can't make the edit work when lightspeed didn't think it's the same entity when the entity(business) was passed through HttpPost to the function. Or am I supposed to do it like this below? (Is there a better way like how EF code didn't need any assignment at all?)
using (var uow = context.CreateUnitOfWork()) { if (ModelState.IsValid) { uow.Properties.First(p => p.Id == property.Id).name = property.name; uow.SaveChanges(); isEdited = true; } }
|
|
|
In LightSpeed 3, unlike EF, you can't send entities over the wire, you can only send DTOs (which you can readily map to entities). We have some major improvements to this coming up in LightSpeed 4 which do allow distributed entities in what I believe is a more EF-like way. (The LightSpeed 4 beta is available now but only to customers with a current licence.) Assuming that you are on LightSpeed 3, therefore, the entity must be loaded through the same unit of work that you save. So in your code fragment: uow.Properties.First(p => p.Id) returns a Property entity which is part of the uow unit of work. You then modify that entity by setting its name. So when you call uow.SaveChanges, uow contains a now modified Property entity, which results in an UPDATE to the database. You may be able to avoid doing explicit assignment by using the designer-generated DTOs or the ASP.NET MVC model binder depending on whether this is a WCF service or a MVC app. If you can let us know which it is then we can point you in the right direction. |
|
|
Wow, thanks! I'm doing MVC + LightSpeed 3 for now. So, model binder? I need to read up on that. :x
Also, mind giving an example of the LightSpeed 4 (or the intended design if it hasn't been implemented yet) version of the code? Not sure what you mean by EF-like. |
|
|
On the distributed enhancements in LightSpeed 4, have a look at these two blog posts to get an overview:
Jeremy |
|
|
Hi, Please find attached a zip file containing:
If you have any questions we're happy to help, John-Daniel Trask |
|
|
Thanks a lot!
> If you have any questions we're happy to help, Will do. |
|
|
! The readme.txt is empty! :O |
|
|
Whoops! I forgot to hit save before adding it to the zip. How embarrassing. Here it is attached. John-Daniel Trask |
|