Good noon everybody!
I am looking to update the property of an object. What I want to do is set the viewmodel prop “CurrentAddress” to “Address” if they are not equal to one another. The current address is hidden and is not to be displayed in either the front end or in the back office of the application. Just update the value current address if address is changed.
The problem is I don’t see an easy way to update values in the database through code as far as I can tell. Other services provide rather straight forward CRUD operations. However all of these services can only show the method definitions. Not their implementations.
So I was wondering is there some clear cut way of updating a value directly through code? For example mapping the model to and from something that would allow for the update.
Best regards.
It’s one of our own simply called Workshop. It’s for a page template which has an “Address”, a “CurrentAddress” and “Coordinates” string properties.
What’s supposed to happen really is: Someone makes a change to address in back office: Address != CurrentAddress -> Make Api call, fetch coordinates and set current address to address. Now if the Address isn’t updated we are not making any unnecessary calls to retreive said coordinates. Yet if someone changes the Address, once again the coordinates will be fetched and CurrentAddress is set to the value of Address.
It’s really easy to get values to display in a view for example. I am wondering if there is a convenient way to make updates?? Maybe something along the lines of EF (without my logic impl ofc):
using (var context = new DBEntities())
{
var workShop = context.workShops.First();
workShop.CurrentAddress = workShop.Address;
context.SaveChanges();
}
Using the DataService to fetch pages by their known system identifier is wrong way and will have performance implication because all request is touching database. Instead you should use the PageService.Get(systemId)-method that will use the cache and reduce the load on the system.
You set the value with culture and fetching the value with CultureInfo.CurrentCulture, is the culture and CultureInfo.CurrentCulture the same?
Yes. Though during testing with the debugger i would get the data in the update object but not on page object i think i need to do => _pageService.Update(update)?
Because after that _pageService.Get(pageId) works though for some reason it only worked as long as I was in scope. If I would refresh page in BO or press Publish the next _pageService.Get(pageId) it would be null again.
Now suddenly i get the data without any code changes. But PageModel that gets passed in ModelBuilder Build method that field does not exists and is null when mapped to the ViewModel.
Update:
Now again after closing debugger and starting again i’m getting null after _pageService.Get(pageId)
I have a feeling this is something with cache?
var page = _pageService.Get(pageId);
var update = page.MakeWritableClone();
if (culture != null)
update.Fields.AddOrUpdateValue(fieldId, culture, value);
else
update.Fields.AddOrUpdateValue(fieldId, value);
_pageService.Update(update);