如何修复“不支持类型为基元集合或复杂类型的属性”使用 EF DbContext?

发布于 2024-12-28 07:38:06 字数 2505 浏览 2 评论 0 原文

我有一个包含 POCO 实体的项目。已使用 Entity Framework 4.2 和代码优先为其创建了数据库上下文。这工作正常,但上下文需要作为 OData 服务公开,这是行不通的。

浏览 OData 服务时出现以下错误:

类型上的属性“DataSubmissionItems” 'Lifecycle.ProgramReportSubmission.Model.ProgramReportSubmission' 是 不是有效的财产。类型为集合的属性 不支持基元或复杂类型。

数据服务类看起来像:

public class ExceptionReportDataService : DataService<ExceptionReportEntitiesContext>
{
    // This method is called only once to initialize service-wide policies.
    public static void InitializeService(DataServiceConfiguration config)
    {
        config.SetEntitySetAccessRule("*", EntitySetRights.AllRead);
        config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V2;
        config.UseVerboseErrors = true;
    }
}

EF上下文类看起来像:

public class ExceptionReportEntitiesContext : DbContext
{
    public DbSet<ExceptionReport> ExceptionReports { get; set; }

    public ExceptionReportEntitiesContext()
        : base(DynamicConfig.GetAppSettingValue("DB_CONN_STRING_LIFECYCLE"))
    {
    }
}

POCO实体看起来像:

namespace WBRT.ProgramData.Lifecycle.ExceptionReportModel
{
    public class ExceptionReport
    {
        public virtual Guid ExceptionReportID { get; set; }
        public virtual Lifecycle.ProgramReportSubmission.Model.ProgramReportSubmission ReportSubmission { get; set; }       
    }
}

namespace Lifecycle.ProgramReportSubmission.Model
{           
    public class ProgramReportSubmission
    {
        public Guid ProgramReportSubmissionId { get; set; }
        public virtual ICollection<DataSubmissionItem> DataSubmissionItems { get; set; }
    }

    public class DataSubmissionItem
    {
        public Guid DataSubmissionItemId { get; set; }
    }
}

我尝试过的:

  • 在 DataSubmissionItem 类上设置 DataServiceKey
  • 在 ExceptionReportEntitiesContext 构造函数以及数据服务中将 ProxyCreationEnabled 设置为 false
  • 重写 OnModelCreating 和删除 IncludeMetadataConvention
  • 重写 OnModelCreating 并设置 modelBuilder.Entity().Ignore(prs => prs.DataSubmissionItems);

注意: 我无法在 POCO 实体项目中引入对 EntityFramework DLL 的依赖关系,因为这会影响引用仍运行 .NET 的项目3.5.

有人知道如何解决这个错误吗?

I have a project containing POCO entities. A database context has been created for it using Entity Framework 4.2 and code first. This works fine, but the context needs to be exposed as an OData service which does not work.

Browsing to the OData service gives this error:

The property 'DataSubmissionItems' on type
'Lifecycle.ProgramReportSubmission.Model.ProgramReportSubmission' is
not a valid property. Properties whose types are collection of
primitives or complex types are not supported.

The data service class looks like:

public class ExceptionReportDataService : DataService<ExceptionReportEntitiesContext>
{
    // This method is called only once to initialize service-wide policies.
    public static void InitializeService(DataServiceConfiguration config)
    {
        config.SetEntitySetAccessRule("*", EntitySetRights.AllRead);
        config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V2;
        config.UseVerboseErrors = true;
    }
}

The EF context class looks like:

public class ExceptionReportEntitiesContext : DbContext
{
    public DbSet<ExceptionReport> ExceptionReports { get; set; }

    public ExceptionReportEntitiesContext()
        : base(DynamicConfig.GetAppSettingValue("DB_CONN_STRING_LIFECYCLE"))
    {
    }
}

The POCO entities look like:

namespace WBRT.ProgramData.Lifecycle.ExceptionReportModel
{
    public class ExceptionReport
    {
        public virtual Guid ExceptionReportID { get; set; }
        public virtual Lifecycle.ProgramReportSubmission.Model.ProgramReportSubmission ReportSubmission { get; set; }       
    }
}

namespace Lifecycle.ProgramReportSubmission.Model
{           
    public class ProgramReportSubmission
    {
        public Guid ProgramReportSubmissionId { get; set; }
        public virtual ICollection<DataSubmissionItem> DataSubmissionItems { get; set; }
    }

    public class DataSubmissionItem
    {
        public Guid DataSubmissionItemId { get; set; }
    }
}

What I've tried:

  • Setting DataServiceKey on the DataSubmissionItem class
  • Setting ProxyCreationEnabled to false on the ExceptionReportEntitiesContext constructor as well as in the data service
  • Overriding OnModelCreating and removing the IncludeMetadataConvention
  • Overriding OnModelCreating and setting modelBuilder.Entity<ProgramReportSubmission.Model.ProgramReportSubmission>().Ignore(prs => prs.DataSubmissionItems);

Note: I can't introduce a dependency on the EntityFramework DLL in the POCO entities project as this affects referencing projects that still run .NET 3.5.

Anyone know how to resolve this error?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小梨窩很甜 2025-01-04 07:38:06

WCF DS 的 RTM 版本不支持此类属性。但最新的 CTP 却做到了。 http://blogs.msdn.com/b/astoriateam/archive/2011/10/13/announcing-wcf-data-services-oct-2011-ctp-for-net-4-and- silverlight-4.aspx

另一方面,您收到此类错误的事实可能意味着 WCF DS 无法将提供程序识别为 EF,而是像使用反射提供程序一样使用它。因此,即使是最新的 CTP 也无法真正解决这个问题。

如果 DataService 中的 T 是 ObjectContext 或派生类型,则 WCF DS 当前仅识别 EF 提供程序。 EF Code First 的典型解决方法是将服务定义为 DataService,然后重写其上的 CreateDataSource 方法并从 DbContext 返回 ObjectContext 实现。请参阅这篇文章了解如何执行此操作(它是关于 EF 4.1,但我认为这同样适用于 4.2):http://social.technet.microsoft.com/wiki/contents/articles/5234.aspx。您可以跳到有关 WCF DS 的部分。

THe RTM version of WCF DS doesn't support these kind of properties. But the latest CTP does. http://blogs.msdn.com/b/astoriateam/archive/2011/10/13/announcing-wcf-data-services-oct-2011-ctp-for-net-4-and-silverlight-4.aspx.

On the other hand, the fact that you get such an error probably means that WCF DS doesn't recognize the provider as EF, and istead works with it as with a reflection provider. So even the latest CTP won't really fix that problem.

WCF DS currently only recognizes EF provider if the T in DataService is ObjectContext or derived type. The typical workaround for EF Code First is to define the service as DataService and then override the CreateDataSource method on it and return the ObjectContext implementation from your DbContext. See this article about how to do that (it's about EF 4.1, but I think the same will apply to 4.2 as well): http://social.technet.microsoft.com/wiki/contents/articles/5234.aspx. You can skip down to the part about WCF DS.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文