Do I need to call my domain service's context.SaveChanges after adding a new entity?

99封情书 提交于 2019-12-23 12:08:37

问题


I am building an application based on Ria Services.

Amongst the default methods that were built by the Visual Studio Wizard, I can see the ones that should insert and persist new data.

For example:

    public void InsertCustomer(Customer customer)
    {
        if ((customer.EntityState != EntityState.Detached))
        {
            this.ObjectContext.ObjectStateManager.ChangeObjectState(customer, EntityState.Added);
        }
        else
        {
            this.ObjectContext.Customers.AddObject(customer);
        }
    }

However, I don't see any this.ObjectContext.SaveChanges(), which I read should be invoked in order to insert the entity in the data store. ...do I need to modify these functions in order to persist the information?

Thanks in advance, Cheers, Gianluca.


回答1:


When you call SubmitChanges on the client, it calls into DomainService.Submit on the server. The default implementation then sequentially calls into a number of protected virtual methods including AuthorizeChangeSet, ValidateChangeSet, ExecuteChangeSet, and PersistChangeSet. Your CUD operation will be invoked from ExecuteChangeSet and ObjectContext.SaveChanges() will be invoked from PersistChangeSet.

You don't need to modify the default methods to persist information as that will be taken care of by default. However, the design gives you the option of overriding chunks of the submit pipeline if you find more complex scenarios necessitate it.




回答2:


What you should do is something like this:

//Create an instance of your Domain context in your class.
YourDomainContext context = new YourDomainContext();

if (context.HasChanges)
{
    context.SubmitChanges(so =>
        {
            string errorMsg = (so.HasError) → "failed" : "succeeded";
            MessageBox.Show("Update " + errorMsg);
        }, null);
}
else
{
    MessageBox.Show("You have not made any changes!");
}

Please take a look a this at this article: Using WCF RIA Services Or take a look at this video: Silverlight Firestarter 2010 Session 3 - Building Feature Rich Business Apps Today with RIA Services



来源:https://stackoverflow.com/questions/5003212/do-i-need-to-call-my-domain-services-context-savechanges-after-adding-a-new-ent

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!