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
任何想法?
繼承不直接用於DataContract。您必須指定已知類型才能繼承。 http://msdn.microsoft.com/en-us/magazine/gg598929.aspx – Harikant 2014-12-08 04:28:42
謝謝,你能展開這個嗎?由於我使用實體框架提供程序而不是手動設置DataContracts,因此如何爲客戶指定[KnownType(typeof(Organization))]的等效值? – 2014-12-08 15:41:08
我想你可以去部分班。創建Customer的部分類並應用Knowtype屬性。 – Harikant 2014-12-09 04:13:44