WCF DataServices+Silverlight+实体框架CodeFirst模型对象为null
本文关键字:对象 null 模型 CodeFirst DataServices+Silverlight+ 实体 框架 WCF | 更新日期: 2023-09-27 18:27:46
我有一个WCF DataService,它具有以下CodeFirst模型:
public class Project
{
public int ProjectID { get; set; }
public string Name { get; set; }
public virtual Owner Owner { get; set; }
}
public class Owner
{
public int OwnerID { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
}
我还有标准的支持类SchedulerContext
、SchedulerContextInitializer
和SchedulerService.svc
。我的服务看起来像:
public class SchedulerService : DataService<ObjectContext>
{
public static void InitializeService(DataServiceConfiguration config)
{
config.SetEntitySetAccessRule("*", EntitySetRights.All);
config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V3;
}
protected override ObjectContext CreateDataSource()
{
var ctx = new SchedulerContext();
var objectContext = ((IObjectContextAdapter)ctx).ObjectContext;
return objectContext;
}
}
我很好地将服务引用添加到了我的Silverlight项目中。我可以在浏览器中浏览服务并查看我的种子数据。当我转到http://localhost:31560/SchedulerService.svc/Projects(1)/Owner
时,我可以看到我的种子数据。但是,当我的项目加载到Silverlight数据访问类中时:
public class SchedulerService : ISchedulerService
{
public SchedulerService()
{
var context = new SchedulerContext(new Uri("/SchedulerService.svc", UriKind.Relative));
this.Projects = new DataServiceCollection<Project>();
DataServiceQuery<Product> query = context.Projects;
this.Projects.LoadAsync(query);
}
public DataServiceCollection<Project> Projects { get; private set; }
}
每个项目的所有者总是为空!我该怎么解决这个问题?!
您尝试过吗:
context.Projects.Expand("Owner");