WCF RIA Services & EF - Tip: Silverlight端访问外键关联实体

问题:在Domain Service中使用EF 4 实现如下数据访问,通过Include语句获得Job所关联的Vehicle实体

 

public IQueryable<Job> GetRunsByVehicleDate(int vehicleID, DateTime date)
        {
            date = date.Date;
            DateTime dateNext = date.AddDays(1);
            var result = this.ObjectContext.Jobs.Include("Vehicle").Include("Location").Include("TimeSlot")
                                           .Where(j => j.JobDate >= date && j.JobDate <= dateNext && j.VehicleID == vehicleID)
                                           .OrderBy(j => j.JobDate);
            return result;
         
        }

 

然而在Silverlight端,job.Vehicle 得到的却是null

 

原因:RIA没有将关联实体在客户端代码上正确生成

 

解决:使用Include 属性并修饰Job实体如下,

 

    [MetadataType(typeof(JobMetaData))]
    partial class Job : IEntity
    {

        public class JobMetaData
        {
            [Include]
            public Vehicle Vehicle { get; set; }

            [Include]
            public Location Location { get; set; }
           
        }
    }

 

注意:Include来自 System.ServiceModel.DomainServices.Server.dll at path like

C:/Program Files (x86)/Microsoft SDKs/RIA Services/v1.0/Libraries/Server

你可能感兴趣的:(Date,Class,include,silverlight,WCF,RIA)