【发布时间】: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