开发者

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

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开发者_StackOverflow中文版 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.


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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜