English 中文(简体)
Silverlight+RIA:编辑实体时出现问题
原标题:Silverlight + RIA: problem editing entity

我有一个使用EntityFramework作为数据层的Silverlight 4应用程序。

有两个实体:客户和产品。当我从数据库中获取客户时,相关产品也会被读取,因为我在客户的元数据中添加了相关的Include属性,并在获取查询中调用Include方法:

public IQueryable<customer> GetCustomerSetById(int customerId)
{
    return this.ObjectContext.CustomerSet
        .Include(o => o.Products)
        .Where(o => o.Id = customerId);
}

当我更改客户产品中的任何属性时,我会遇到以下异常:

This EntitySet of Type MyApp.Web.Models.Product does not support the Edit operation.

但是,如果我直接阅读客户产品,一切都会起作用,例如,不是通过客户实体(CustomerContext),而是通过产品一(ProductContext)。

产品实体中还有IsReadOnly=true属性。

更新:

我有所有CUD操作,并用相关的Insert、Update和Delete属性标记了所有操作。否则,它根本不起作用,但在某些情况下,它对我有效,正如我在上面所写的那样。

有什么想法吗?

最佳回答

这是RIA+EF的真正问题,因此我们将所有实体保留在一个域服务中,因为在客户端很难处理通过导航属性相关的多个实体。想一想,它实际上没有什么区别,我们使用EF T4模板在一个类中生成所有域服务操作。我们生成了部分方法来拦截域服务方法的逻辑。

问题回答

听起来你需要确保你的域服务中有更新操作。它看起来像这样:

public void UpdateProduct(Product product)
{
    ObjectContext.Products.AttachAsModified(product, ChangeSet.GetOriginal(product));
}

RIA Services EntitySet不支持Edit操作

由于上述解决方案似乎没有帮助,请尝试使用此解决方案:

域服务向导

This wizard should look at your entity, and generate the appropriate CRUD operations. If you then cant update your entities you have a different problem.

您是否尝试过将Include移到末尾?

Return this.ObjectContext.CustomerSet
        .Include(o => o.Products)
        .Where(o => o.Id = customerId);

可能是:

Return (from o in this.ObjectContext.CustomerSet
        where o.Id = customerId
        select o).Include("Products");




相关问题
Silverlight Rich text box control

Our team decided that we need our own custom Rich text box control for Silverlight app we are developing. We looked at existing controls mentioned at A good rich text control for Silverlight but ...

Silverlight ImageBrush not rendering (with Bing Map Control)

I m trying to add an image to a Pushpin instance from the Silverlight Bing Map Control, but I can t seem to get it to render (the pushpin renders fine). This is probably a general WPF question rather ...

Silverlight OpenFileDialog DoEvents equivalent

I m processing large files after they are selected by the user. My code looks like the following: if (FileDialog.ShowDialog() == true) { // process really big file } This freezes up the UI so ...

list of controls with templates in silverlight

Does anyone know where to find a list of controls that you can set the template on in Silverlight? I ve wasted several hours now trying to create control templates only to find that the control doesn ...

Silverlight, Updating the UI during processing

I have a simple silverlight multifile upload application, and i want to provide the user with some feedback, right now its only in a test phase and i dont have the webservice. Somehow i cant get the ...

Silverlight 3 - FindName returns null

This looks a bug to me.. Using Silverlight 3 and i have a user control defined in XAML and trying to access the object during runtime returns a null. <Grid> <common:CommonGridEditPanel x:...

silverlight 3 collection binding

Someone please help me understand why this binding does not work... I have a class called SelectionManager with a property called dates which is populated by a WCF service. The property is an ...

热门标签