【问题标题】:WCF Data Services Entity Framework Provider fails to recognize inheriting entitiesWCF 数据服务实体框架提供程序无法识别继承实体
【发布时间】:2014-12-08 02:36:22
【问题描述】:

我首先使用实体​​框架(版本 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; }
}

以及使用 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);
    }
}

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

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

有什么想法吗?

【问题讨论】:

  • 继承不能直接用于 DataContract。您必须指定已知类型才能进行继承。 msdn.microsoft.com/en-us/magazine/gg598929.aspx
  • 谢谢,你能扩展一下吗?由于我使用的是实体框架提供程序而不是手动设置 DataContracts,我如何为客户指定 [KnownType(typeof(Organization))] 的等效项?
  • 我想你可以参加部分课程。创建客户的部分类,然后应用 Knowtype 属性。

标签: c# inheritance ef-code-first entity-framework-6 wcf-data-services


【解决方案1】:

这是根据这个 msdn 论坛 post 设计的:

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

因此,我更新后的 WCF 服务只需要包含基本类型,并确保正在使用数据服务协议的版本 3,以便使用 OfType&lt;&gt; 等,以便能够查询和更新扩展实体:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-13
    • 1970-01-01
    • 1970-01-01
    • 2011-05-19
    • 2010-10-07
    相关资源
    最近更新 更多