1

我首先使用實體​​框架(版本6.1.1)代碼,並且WCF數據服務實體框架提供程序(仍在1.0.0-beta2預發佈中)提供服務。WCF數據服務實體框架提供程序無法識別繼承實體

此設置適用於沒有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 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 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 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 

任何想法?

+0

繼承不直接用於DataContract。您必須指定已知類型才能繼承。 http://msdn.microsoft.com/en-us/magazine/gg598929.aspx – Harikant 2014-12-08 04:28:42

+0

謝謝,你能展開這個嗎?由於我使用實體框架提供程序而不是手動設置DataContracts,因此如何爲客戶指定[KnownType(typeof(Organization))]的等效值? – 2014-12-08 15:41:08

+0

我想你可以去部分班。創建Customer的部分類並應用Knowtype屬性。 – Harikant 2014-12-09 04:13:44

回答

0

這是由設計按照此MSDN論壇post

這是一個由設計功能的恢復體力提供商(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); 
    } 
}