Silvelight& RIA Services& Entity Framework存在部分类和复杂类型的问题

时间:2012-10-03 08:35:05

标签: c# silverlight entity-framework wcf-ria-services

我正在使用带有Entity Framework的RIA服务和Silverlight作为客户端应用程序。我通过部分类为EF Entity提供了一些自定义属性。数据库中有一个XML类型的字段,它作为字符串映射到Entity Framework。我使用partial class将这个xml字符串反序列化为真实对象。

这是EF配置实体的部分类:

public partial class Configuration
{
    private ServiceCredentials _serviceCredentialsObject;

    [DataMember]
    public ServiceCredentials ServiceCredentialsObject
    {
        get
        {
            return this._serviceCredentialsObject
                   ?? (this._serviceCredentialsObject = this.DeserializeServiceCredentialsToObject());
        }

        set
        {
            this._serviceCredentialsObject = value;
            this.SerializeServiceCredentialsObject();
        }
    }

    public ServiceCredentials DeserializeServiceCredentialsToObject()
    {
        if (string.IsNullOrEmpty(this.ServiceCredentials))
        {
            return null;
        }

        var result = XmlSerializerHelper.Deserialize<ServiceCredentials>(this.ServiceCredentials);
        result.FileEncoding = result.FileEncoding ?? Encoding.UTF8;

        return result;
    }

    public void SerializeServiceCredentialsObject()
    {
        if (this.ServiceCredentialsObject == null)
        {
            this.ServiceCredentials = null;
            return;
        }

        this.ServiceCredentials = XmlSerializerHelper.Serialize(this.ServiceCredentialsObject);
    }

}

这是我试图反序列化的对象:

[Serializable]
public class ServiceCredentials
{

    public NetworkCredential Credential { get; set; }

    public Encoding FileEncoding { get; set; }

    [XmlIgnore]
    public long HistoryID { get; set; }

    public string LoadFileStoragePath { get; set; }

    public string ManualLoadFilePath { get; set; }

    public bool NeedAuthorization { get; set; }

    [XmlIgnore]
    public string ProviderID { get; set; }

    public string SourceUrl { get; set; }

    public bool AutomaticTransferToProductive { get; set; }
}

当我尝试在Silverlight客户端使用配置实体生成代码时,发现Configuration类中没有ServiceCredentialsObject的问题。如果我创建一个新的,它不会添加到DomainService.metadata.cs。如果我手动将ServiceCredentialsObject添加到DomainService.metadata.cs,我可以在重建后在clien端访问它,但我只能在那里找到具有简单类型的属性。例如,一个可以访问HistoryID,SourceUrl,AutomaticTransferToProductive但是没有为

生成的属性

public NetworkCredential Credential {get;组; }   public Encoding FileEncoding {get;组; }

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

我找到了解决这个问题的唯一方法。解决方案只是在客户端从实体框架反序列化xml字段。我已经为silverlight中的生成代码创建了部分类配置。我不知道这是否是最佳解决方案,但它确实有效。