WCF OData服务和EF 6问题 - 无法使用Odata服务公开实体

 我从不在乎O心痛 发布于 2022-12-21 10:08

我正在使用WCF数据服务(Odata)与.NET framework 4.5.1和EF 6.1.我已经使用代码第一种方法来创建EF模型.当我将此EF模型(AddProjectModel.cs)引用到WCF OData服务(WcfDataService1.svc)时,它会抛出以下错误:

错误:

服务器遇到处理请求的错误.异常消息是'在数据上下文类型'AddProjectModel',有一个顶级IQueryable属性'Assets',其元素类型不是实体类型.确保IQueryable属性是实体类型,或者在数据上下文类型上指定IgnoreProperties属性以忽略此属性.请参阅服务器日志以获取更多详 异常堆栈跟踪是:

在System.Data.Services.Providers.ReflectionServiceProvider.PopulateMetadata(ProviderMetadataCacheItem metadataCacheItem)处于System.Data.Services.DataService的System.Data.Services.Providers.BaseServiceProvider.LoadMetadata(Boolean skipServiceOperations)1.CreateInternalProvider(Object dataSourceInstance) at System.Data.Services.DataService1.CreateMetadataAndQueryProviders(IDataServiceMetadataProvider&metadataProviderInstance,IDataServiceQueryProvider&queryProviderInstance,对象&dataSourceInstance,布尔逻辑isInternallyCreatedProvider)在System.Data.Services.DataService 1.CreateProvider() at System.Data.Services.DataService1.HandleRequest()在System.Data.Services.DataService`1.ProcessRequestForMessage(流消息体)在SyncInvokeProcessRequestForMessage(对象,对象[],在系统对象[]) System.ServiceModel.Dispatcher.DispatchOperationRuntime.InvokeBegin(MessageRpc&rpc)at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage5(MessageRpc&rpc)中的.ServiceModel.Dispatcher.SyncMethodInvoker.Invoke(Object instance,Object [] inputs,Object []&outputs) )在System.ServiceModel.Dispatcher.Immuta 位于System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage31(MessageRpc&rpc)的System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage4(MessageRpc&rpc)上的bleDispatchRuntime.ProcessMessage41(MessageRpc&rpc),位于System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage3(MessageRpc&rpc)at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage2(MessageRpc&RPC)在System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage11在System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage1在System.ServiceModel.Dispatcher.MessageRpc(MessageRpc&RPC)(MessageRpc&RPC).进程(Boolean isOperationContextSet)

这是我的WCF数据服务:WcfDataService1.svc

namespace AddProjectService
{
 public class WcfDataService1 : DataService
 {
    // This method is called only once to initialize service-wide policies.
    public static void InitializeService(DataServiceConfiguration config)
    {
        // TODO: set rules to indicate which entity sets and service operations are visible,
         updatable, etc.
        // Examples:
        config.SetEntitySetAccessRule("*", EntitySetRights.AllRead);
        config.SetServiceOperationAccessRule("*", ServiceOperationRights.All);
        config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V3;
    }
 }
}

我的代码优先模型:AddProjectModel.cs

 public partial class AddProjectModel : DbContext
 {
  public AddProjectModel()
    : base("name=AddProjectModel")
  {
  }

  public virtual DbSet Assets { get; set; }
  public virtual DbSet Projects { get; set; }

  protected override void OnModelCreating(DbModelBuilder modelBuilder)
  {
    modelBuilder.Configurations.Add(new AssetMap());
    modelBuilder.Configurations.Add(new ProjectMap());   
  }         
 }  

 public class AssetMap : EntityTypeConfiguration
 {
  public AssetMap()
  {
  this.Property(a => a.AssetId).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);      
  this.HasMany(a => a.Projects).WithRequired(p => p.Asset).HasForeignKey(p => p.AssetId);

  //table  & column mappings
  this.ToTable("TBLASSET");
  this.Property(a => a.AssetId).HasColumnName("ASSETID");
  this.Property(a => a.AssetLevelId).HasColumnName("ASSETLEVELID");
  this.Property(a => a.AssetNumber).HasColumnName("ASSETNUMBER");
  this.Property(a => a.Name).HasColumnName("NAME");
  this.Property(a => a.AssetTypeId).HasColumnName("ASSETTYPEID");      
 }
}  

public class ProjectMap : EntityTypeConfiguration
{
  public ProjectMap()
  {
   this.Property(p => p.ProjectId).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
   this.HasMany(p => p.SchedulePhases).WithRequired(sp => sp.Project).HasForeignKey(sp => 
   sp.ProjectId); 

  //table & column mappings
  this.ToTable("TBLPROJECT");
  this.Property(p => p.ProjectId).HasColumnName("PROJECTID");
  this.Property(p => p.AssetId).HasColumnName("ASSETID");
  this.Property(p => p.CapitalCategoryId).HasColumnName("CAPITALCATEGORYID");
  this.Property(p => p.Comments).HasColumnName("COMMENTS");
  this.Property(p => p.DesignationId).HasColumnName("DESIGNATIONID");
  this.Property(p => p.DispositionId).HasColumnName("DISPOSITIONID");
  this.Property(p => p.FMSNumber).HasColumnName("FMSNUMBER");
  this.Property(p => p.FundingSourceId).HasColumnName("FUNDINGSOURCEID");
  this.Property(p => p.ImplementerId).HasColumnName("IMPLEMENTERID");
  this.Property(p => p.IsApproved).HasColumnName("ISAPPROVED");
  this.Property(p => p.IsDeferred).HasColumnName("ISDEFERRED");
  this.Property(p => p.IsLongTermLease).HasColumnName("ISLONGTERMLEASE");
  this.Property(p => p.IsRollover).HasColumnName("ISROLLOVER");
  this.Property(p => p.IsSidewalkBridge).HasColumnName("ISSIDEWALKBRIDGE");
  this.Property(p => p.JobDescription).HasColumnName("JOBDESCRIPTION");
  this.Property(p => p.JobType).HasColumnName("JOBTYPE");
  this.Property(p => p.OrganizationId).HasColumnName("ORGANIZATIONID");
  this.Property(p => p.ProgramCategoryId).HasColumnName("PROGRAMCATEGORYID");
  this.Property(p => p.DsfId).HasColumnName("DSFID");
  this.Property(p => p.StatusId).HasColumnName("STATUSID");

  this.Map(m => m.Requires("PROJECTTYPEID").HasValue(15))
      .Map(m => m.Requires("PROJECTTYPEID").HasValue(2));
 }
}

资产类别:

public class Asset
{
 public Asset()
 {
   Projects = new HashSet();      
 }

 [Key]
 public decimal AssetId { get; set; }

 [StringLength(20)]
 public string AssetNumber { get; set; }

 [StringLength(100)]
 public string Name { get; set; }

 public decimal? AssetLevelId { get; set; }

 public decimal? AssetTypeId { get; set; }

 public virtual ICollection Projects { get; set; }    
}

项目类:

public class Project
{
 public Project()
 {      
 } 

 [Key]
 public decimal ProjectId { get; set; }

 public decimal AssetId { get; set; }

 public decimal CapitalCategoryId { get; set; }

 //public decimal ProjectTypeId { get; set; }

 public decimal ProgramCategoryId { get; set; }   

 [StringLength(1024)]
 public string Comments { get; set; }

 public decimal ImplementerId { get; set; }

 public decimal StatusId { get; set; }

 public decimal DsfId { get; set; }

 [StringLength(20)]
 public string FMSNumber { get; set; }

 [StringLength(1024)]
 public string JobDescription { get; set; }

 [StringLength(2)]
 public string JobType { get; set; }

 public decimal OrganizationId { get; set; }

 [Required][StringLength(1)]
 public string IsRollover { get; set; }

 [Required][StringLength(1)]
 public string IsDeferred { get; set; }

 [Required][StringLength(1)]
 public string IsApproved { get; set; }

 [StringLength(1)]
 public string IsSidewalkBridge { get; set; }

 public decimal FundingSourceId { get; set; }

 public decimal? DesignationId { get; set; }

 public decimal? DispositionId { get; set; }

 [Required][StringLength(1)]
 public string IsLongTermLease { get; set; }

 public virtual Asset Asset { get; set; }   
}

我不知道如何解决这个问题.你能告诉我我在这里失踪了吗?

我正在使用oracle数据库,我们最近从devart购买了针对oracle的dotConnect的许可证.

谢谢,


嗨,

我通过在每个POCO类上设置带有主键的[DataServiceKey]属性来解决此错误.请参考:http://blog.marcgravell.com/2008/12/astoria-and-linq-to-sql-getting-started.html.

现在我可以通过Odata服务公开实体,但是当我尝试通过键入URL(例如.../WcfDataService1.svc/Assets)来访问实体集合时,它会抛出以下错误:

错误:

  
 
    
   An error occurred while processing this request. 
   
     Operation could destabilize the runtime. 
     System.Security.VerificationException 
     at queryable_reader(Object ) at   System.Data.Services.Providers.ReflectionServiceProvider.GetQueryRootForResourceSet(ResourceSet container) at System.Data.Services.Providers.ReflectionDataServiceProvider.GetQueryRootForResourceSet(ResourceSet resourceSet) at System.Data.Services.Providers.DataServiceProviderWrapper.GetQueryRootForResourceSet(ResourceSetWrapper resourceSet) at System.Data.Services.RequestUriProcessor.ComposeExpressionForEntitySet(SegmentInfo segment, IDataService service, Boolean isLastSegment, Boolean checkRights) at System.Data.Services.RequestUriProcessor.ComposeExpressionForSegments(IList`1 segments, IDataService service, Boolean isCrossReferencingUri) at System.Data.Services.RequestUriProcessor.ProcessRequestUri(Uri absoluteRequestUri, IDataService service, Boolean internalQuery) at System.Data.Services.DataService`1.ProcessIncomingRequestUri() at System.Data.Services.DataService`1.HandleRequest() 
  
 

我该如何解决这个问题?

谢谢,

撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有