[英]Copy values between objects
我正在编写一个实用程序,用于将特定数据从后端SQL数据库复制到客户端计算机SQL Express数据库。 后端数据库和客户端数据库是相同的。 数据适用于没有网络就可以到达远程站点的测量师。 我正在使用REST服务,并且在服务和代理上都使用了Entity Framework。 我使用以下代码复制属性值:
private void GatherFrom<TSelf, TSource>(TSelf self, TSource source)
{
PropertyInfo[] sourceAllProperties = source.GetType().GetProperties();
foreach (PropertyInfo sourceProperty in sourceAllProperties)
{
PropertyInfo selfProperty = self.GetType().GetProperty(sourceProperty.Name);
if (selfProperty.CanRead
&& (selfProperty.GetSetMethod(true) != null && !selfProperty.GetSetMethod(true).IsPrivate)
&& (selfProperty.GetSetMethod().Attributes & MethodAttributes.Static) == 0
&& selfProperty.PropertyType.IsAssignableFrom(sourceProperty.PropertyType))
{
var sourceValue = sourceProperty.GetValue(source);
selfProperty.SetValue(self, sourceValue);
}
}
}
一切正常。
但是当我应用新数据时:
Surveys newSurvey = new Surveys();
GatherFrom(newSurvey, survey);
localSurveys.Add(newSurvey);
我遇到了问题,因为在同一个名称空间中,远程和本地的类型不明确。
知道如何拆分吗?
您只需要指定模棱两可的对象的完整名称空间。 例如:
LocalNamespace.Something.Surveys localSurveys;
RemoteNamespace.Something.Surveys remoteSurveys;
您还可以使用别名导入名称空间:
using Local = LocalNamespace.Something;
using Remote = RemoteNamespace.Something;
Local.Surveys localSurveys;
Remote.Surveys remoteSurveys;
不仅如此简单! 首先,我将代码生成策略从T4更改为Legacy ObjectContent。 这可以在实体框架模型图中完成。 我在服务设置的两端都这样做了。 请记住删除嵌套在.edmx下的两个.tt文件。 然后,将.edmx(关闭模型窗口)的自定义工具命名空间设置为其他命名空间。
那对我有用:-)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.