gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 04:45:29 29 4
gpt4 key购买 nike

我正在使用带有 .NET 框架 4.5.1 和 EF 6.1 的 WCF 数据服务 (Odata)。我使用代码优先的方法来创建 EF 模型。当我将此 EF 模型 (AddProjectModel.cs) 引用到 WCF OData 服务 (WcfDataService1.svc) 时,它会引发以下错误:

错误:

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

在 System.Data.Services.Providers.ReflectionServiceProvider.PopulateMetadata(ProviderMetadataCacheItem metadataCacheItem) 在 System.Data.Services.Providers.BaseServiceProvider.LoadMetadata(Boolean skipServiceOperations) 在 System.Data.Services.DataService 1.CreateInternalProvider(Object dataSourceInstance) at System.Data.Services.DataService 1.在 System.Data.Services.DataService 1.CreateProvider() at System.Data.Services.DataService 处创建MetadataAndQueryProviders(IDataServiceMetadataProvider& metadataProviderInstance, IDataServiceQueryProvider& queryProviderInstance, Object& dataSourceInstance, Boolean& isInternallyCreatedProvider) 1.HandleRequest() 在 System.Data.Services.DataService`1.ProcessRequestForMessage(Stream messageBody) 在 SyncInvokeProcessRequestForMessage(Object , Object[] , Object[] ) 在 System.ServiceModel.Dispatcher.SyncMethodInvoker.Invoke(Object instance, Object[ ] 在 System.ServiceModel.Dispatcher.DispatchOperationRuntime.InvokeBegin(MessageRpc& rpc) 在 System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage5(MessageRpc& rpc) 在 System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage41(MessageRpc& rpc) 的输入,Object[]& 输出) ) 在 System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage4(MessageRpc& rpc) 在 System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage31(MessageRpc& rpc) 在 System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage3(MessageRpc& rpc) 在 System.ServiceModel.Dispatcher。 System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage11 处的 ImmutableDispatchRuntime.ProcessMessage2(MessageRpc& rpc)( MessageRpc& rpc) 在 System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage1(MessageRpc& rpc) 在 System.ServiceModel.Dispatcher.MessageRpc.Process(Boolean isOperationContextSet)

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

namespace AddProjectService
{
public class WcfDataService1 : DataService<AddProjectModel>
{
// 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<Asset> Assets { get; set; }
public virtual DbSet<Project> Projects { get; set; }

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

public class AssetMap : EntityTypeConfiguration<Asset>
{
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<Project>
{
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<DomainObjectModel.ObjectModel.Project.ProjectCIP>(m => m.Requires("PROJECTTYPEID").HasValue(15))
.Map<DomainObjectModel.ObjectModel.Project.ProjectCapacity>(m => m.Requires("PROJECTTYPEID").HasValue(2));
}
}

Assets 类别:
public class Asset
{
public Asset()
{
Projects = new HashSet<Project>();
}

[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<Project> 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 购买了 dotConnect for oracle 的许可证。

谢谢,

你好,

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

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

错误:
 <?xml version="1.0" encoding="utf-8" ?> 
<m:error xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata">
<m:code />
<m:message xml:lang="en-US">An error occurred while processing this request.</m:message>
<m:innererror>
<m:message>Operation could destabilize the runtime.</m:message>
<m:type>System.Security.VerificationException</m:type>
<m:stacktrace>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()</m:stacktrace>
</m:innererror>
</m:error>

我该如何解决这个问题?

谢谢,

最佳答案

要将 WCF DataService 与 EF6 一起使用,还需要做一些额外的工作。详情请查看以下两篇博文:

Using WCF Data Services 5.6.0 with Entity Framework 6+

WCF Data Services Entity Framework Provider is updated with WCF Data Service 5.6.2

一般来说,您需要以下两个步骤:

  • 安装最新的 Nuget 包 Microsoft.OData.EntityFrameworkProvider按照该页面上的指南进行操作;
  • 将 DataService 替换为 EntityFrameworkDataService,例如在您的 WcfDataService1.svc 中:

    公共(public)类 WcfDataService1 : EntityFrameworkDataService
  • 关于entity-framework - WCF OData 服务和 EF 6 问题 - 无法使用 Odata 服务公开实体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25873290/

    29 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com