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

本文关键字:实体 识别 继承 程序 数据 服务 框架 WCF | 更新日期: 2023-09-27 18:33:32

我首先使用实体框架(版本 6.1.1)代码和 WCF 数据服务实体框架提供程序(仍处于 1.0.0-beta2 预发行版)来提供服务。

此设置适用于没有从其他类继承的 EF 类的应用程序。

但是,我

有一个应用程序,我正在实现每个类型的表 (TPT) 继承。 请考虑以下代码优先类:

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

它们以每种类型的方式在表中映射为:

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");
    }
}

和上下文类:

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; }
}

以及使用实体框架提供程序的 WCF 数据服务:

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

当服务初始化时,我收到以下错误,其中找不到实体集,即使它是在上下文类中定义的:

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

有什么想法吗?

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

这是根据

此 msdn 论坛帖子的设计:

这是反射提供者(WCF 数据)设计的功能 服务提供程序)旨在遍历所有公共类型/属性, 反射提供程序只能为每种类型公开一个实体集 等级制度。它不支持所谓的"MEST"(多个实体集 每个类型),因为它不知道该选择哪一个。

因此,我更新的 WCF 服务只需要包含基类型,并确保使用数据服务协议的版本 3 以便使用 OfType<> 等来查询和更新扩展实体:

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