简体   繁体   English

在Silverlight和WPF客户端之间共享数据模型

[英]Sharing a data model between Silverlight and WPF client

I am using EntityFramework Code First 5 and I have my data model. 我正在使用EntityFramework Code First 5,并且有我的数据模型。

Example: 例:

[Table("Contract"]]
public class Contract
{
    ...

    [ForeignKey("SomeKey")]
    //[Include]
    public virtual BusinessPartner BP
    {
    ...
    }
}

The data model is currently used by WPF client and it works great. WPF客户端当前使用的数据模型非常有效。 Now I am writing a Silverlight client and using the same model. 现在,我正在编写Silverlight客户端并使用相同的模型。 To acces the database I am using RIA Services. 要访问数据库,我正在使用RIA Services。 It worked great in silverlight until I had to add some RIA annotations like [Include] (it is commented in the above example). 直到我必须添加一些RIA注释(如[Include])(在上面的示例中进行了注释),它在Silverlight中的效果很好。

The problem is that we are using .NET 4 Client Profile in our data model and we cannot change it. 问题在于,我们在数据模型中使用的是.NET 4客户端配置文件,我们无法更改它。 But RIA annotations are in System.ServiceModel.DomainServices.Server namespace which requires .NET 4 or .NET 4.5. 但是RIA注释位于System.ServiceModel.DomainServices.Server命名空间中,该命名空间需要.NET 4或.NET 4.5。

So if I add RIA [Include] annotation the model does not compile anymore. 因此,如果我添加RIA [Include]注释,该模型将不再编译。

Is there any way to use RIA annotation attributes witihin .NET 4 Client Profile so I could use the same data model in WPF and Silverlight client? 有什么方法可以在.NET 4客户端配置文件中使用RIA注释属性,以便在WPF和Silverlight客户端中使用相同的数据模型?

I have read something about defining those RIA attributes in XML file but I cannot find an example.. 我已经阅读了一些有关在XML文件中定义这些RIA属性的信息,但是找不到示例。

Thank you 谢谢

I have found a solution to my problem. 我已经找到解决问题的办法。 I have used FluentMetadata, Fluent API for WCF RIA Services that enabled me to define annotations in a different assembly. 我使用了FluentMetadata,它是用于WCF RIA服务的Fluent API,使我能够在其他程序集中定义注释。 It was just what I needed. 这正是我所需要的。 More information about FluentMetadata can be found on the following link 有关FluentMetadata的更多信息,请参见以下链接

If it worked for WPF and works fine in Silverlight the only thing you need is to create your own dummy attribute for you WPF application which will mimic IncludeAttribute from RIA to get it compiled. 如果它适用于WPF并在Silverlight中正常运行,那么您唯一需要为WPF应用程序创建自己的虚拟属性,该属性将模仿RIA中的IncludeAttribute进行编译。 To achieve that you need to place the attribute into same namespace where it is in RIA. 为此,您需要将属性放置在RIA中相同的名称空间中。

namespace System.ServiceModel.DomainServices.Server 
{
    // Just put this into your WPF app :)
    [AttributeUsageAttribute(AttributeTargets.Property|AttributeTargets.Field, AllowMultiple = true, Inherited = true)]
    public class IncludeAttribute : Attribute {}
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM