我使用带有实体框架和 Silverlight 的 RIA 服务作为客户端应用程序。我通过部分类为 EF 实体提供了一些自定义属性。数据库中有一个 XML 类型的字段,它作为字符串映射到实体框架。我使用部分类将这个 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 客户端上使用配置实体和生成的代码时,发现配置类中没有 ServiceCredentialsObject 的问题。如果我创建新的,它不会添加到 DomainService.metadata.cs 中。如果我手动将 ServiceCredentialsObject 添加到 DomainService.metadata.cs 我可以在重建后在客户端访问它,但我只能在那里找到具有简单类型的属性。例如,可以访问 HistoryID、SourceUrl、AutomaticTransferToProductive,但没有为
公共 NetworkCredential 凭证 { 获取;放; } 公共编码 FileEncoding { get; 放; }
我怎样才能解决这个问题?