简体   繁体   English

WCF数据服务实体框架提供程序无法识别继承实体

[英]WCF Data Services Entity Framework Provider fails to recognize inheriting entities

I'm using Entity Framework (version 6.1.1) code first and the WCF Data Services EntityFramework Provider (still in 1.0.0-beta2 prerelease) to provide the services. 我首先使用Entity Framework(版本6.1.1)代码,并使用WCF数据服务EntityFramework Provider(仍在1.0.0-beta2预发行版中)提供服务。

This setup works fine for applications where there are no EF classes that inherit from others. 此设置适用于没有从其他类继承的EF类的应用程序。

However, I have an application where I am implementing table per type (TPT) inheritance. 但是,我有一个应用程序,在其中我要实现每个类型的表(TPT)继承。 Consider the following code first classes: 请考虑以下代码第一类:

public class Customer
{
    public Guid CustomerID { get; set; }

    public string CustomerName { get; set; }
}

public class Organization : Customer
{
    public string OrganizationName { get; set; }
}

These are mapped in a table per type fashion as: 这些以每种类型的方式在表中映射为:

public class CustomerMap : EntityTypeConfiguration<Customer>
{
    public  CustomerMap()
    {
        this.HasKey(t => t.CustomerID);
        this.Property(t => t.CustomerID).HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);

        this.Property(t => t.CustomerName)
            .IsRequired()
            .HasMaxLength(255);

        this.ToTable("tblCustomers_Customer");
        this.Property(t => t.CustomerID).HasColumnName("CustomerID");
        this.Property(t => t.CustomerName).HasColumnName("CustomerName");
    }
}

public class OrganizationMap : EntityTypeConfiguration<Organization>
{
    public  OrganizationMap()
    {
        this.HasKey(t => t.CustomerID);
        this.Property(t => t.CustomerID).HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);

        this.Property(t => t.OrganizationName)
            .IsRequired()
            .HasMaxLength(255);

        this.ToTable("tblCustomers_Organization");
        this.Property(t => t.CustomerID).HasColumnName("CustomerID");
    }
}

And the context class: 和上下文类:

public class ModelContext: DbContext
{
    static ModelContext()
    {
        Database.SetInitializer(new MigrateDatabaseToLatestVersion<ModelContext, Migrations.Configuration>());
    }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Configurations.Add(new CustomerMap());
        modelBuilder.Configurations.Add(new OrganizationMap());
    }

    public DbSet<Customer> Customers { get; set; }

    public DbSet<Organization> Organizations { get; set; }
}

And the WCF data service using the Entity Framework Provider: 以及使用实体框架提供程序的WCF数据服务:

public class EFDS : EntityFrameworkDataService<ModelContext>
{
    public static void InitializeService(DataServiceConfiguration config)
    {
        config.SetEntitySetAccessRule("Customers", EntitySetRights.All);
        config.SetEntitySetAccessRule("Organizations", EntitySetRights.All);
    }
}

When the service is initializing, I get the following error, in which the entity set is not found, even though it is defined in the context class: 服务初始化时,出现以下错误,即使在上下文类中定义了实体集,也未找到:

The given name 'Organizations' was not found in the entity sets.
Parameter name: name

Any ideas? 有任何想法吗?

This is by design as per this msdn forum post : 这是根据此msdn论坛帖子设计

This is a by designed feature that the refection provider(WCF Data Service provider) is designed to walk all public types/properties, the reflection provider can only expose one entity set for each type hierarchy. 这是一种预先设计的功能,即复制提供程序(WCF数据服务提供程序)旨在遍历所有公共类型/属性,反射提供程序只能为每个类型层次结构公开一个实体集。 It doesn't support so called "MEST" (Multiple Entity Sets per Type), because it would not know which one to pick. 它不支持所谓的“ MEST”(每种类型有多个实体集),因为它不知道选择哪个。

So, my updated WCF service needs to include only the base types, and make sure version 3 of the data service protocol is being used in order to use OfType<> , etc. to be able to query and update extending entities: 因此,我更新的WCF服务仅需要包含基本类型,并确保使用了数据服务协议的版本3,以便使用OfType<>等,以便能够查询和更新扩展实体:

public class EFDS : EntityFrameworkDataService<ModelContext>
{
    public static void InitializeService(DataServiceConfiguration config)
    {
        config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V3;
        config.SetEntitySetAccessRule("Customers", EntitySetRights.All);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM