C# Entity Framework 代码优先抽象继承
C# Entity Framework Code First Abstract Inheritance
在接下来的 类 中,我尝试使用抽象基础 类 进行代码优先设置。抱歉,如果这是一个重复的问题。我确实尝试搜索网站并 google 寻找答案,但我尝试过的都没有奏效。
public abstract class IDObject
{
[Key]
public Int32 ID { get; internal set; }
}
public abstract class NamedObject : IDObject
{
public String Name { get; set; }
}
public abstract class HWObject : NamedObject
{
public Double Free { get; set; }
public Double Max { get; set; }
public Double Used { get; set; }
}
public abstract class CPUObject : HWObject { }
public abstract class MemoryObject : HWObject { }
public abstract class StorageObject : HWObject { }
public class Server : NamedObject
{
[JsonConstructor]
internal Server() { }
public String CompanyName { get; set; }
public Boolean IsRunning { get; set; }
public CPUObject CPU { get; set; }
public MemoryObject Memory { get; set; }
public StorageObject Storage { get; set; }
}
我有以下 DBContext:
public class DataContext : DbContext
{
public DataContext() : base("name=DataContext") { }
public DbSet<Server> Servers { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
}
}
当我尝试查看只读数据模型时出现以下错误:
System.InvalidOperationException: The abstract type 'MyApp.CPUObject' has no mapped descendants and so cannot be mapped. Either remove 'MyApp.CPUObject' from the model or add one or more types deriving from 'MyApp.CPUObject' to the model.
如何更改 DBContext 以使错误消失?
编辑:
根据下面的回答,我已经从 CPUObject、MemoryObject 和 StorageObject 中删除了抽象,因为它们本不应该是抽象的。
我已将 DBContext 更改为以下内容:(映射创建一对一的关系)
public class DataContext : DbContext
{
public DataContext() : base("name=DataContext") { }
public DbSet<Server> Servers { get; set; }
public DbSet<CPUObject> CPUObjects { get; set; }
public DbSet<MemoryObject> MemoryObjects { get; set; }
public DbSet<StorageObject> StorageObjects { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Server>().HasRequired(u => u.CPU).WithRequiredDependent().Map(m => m.MapKey("CPUID"));
modelBuilder.Entity<Server>().HasRequired(u => u.Memory).WithRequiredDependent().Map(m => m.MapKey("MemoryID"));
modelBuilder.Entity<Server>().HasRequired(u => u.Storage).WithRequiredDependent().Map(m => m.MapKey("StorageID"));
}
}
CPUObject
目前是一个抽象class,无法实例化。只需删除 abstract
即可使其成为具体对象。然后,将 public DbSet<CPUObject> CPUs { get; set; }
添加到您的 DBContext
您当前收到错误是因为 EF 正在寻找一个具体的 class,因此该错误表明您可以创建另一个继承(即 'deriving from')CPUObject[的具体 class =14=]
在接下来的 类 中,我尝试使用抽象基础 类 进行代码优先设置。抱歉,如果这是一个重复的问题。我确实尝试搜索网站并 google 寻找答案,但我尝试过的都没有奏效。
public abstract class IDObject
{
[Key]
public Int32 ID { get; internal set; }
}
public abstract class NamedObject : IDObject
{
public String Name { get; set; }
}
public abstract class HWObject : NamedObject
{
public Double Free { get; set; }
public Double Max { get; set; }
public Double Used { get; set; }
}
public abstract class CPUObject : HWObject { }
public abstract class MemoryObject : HWObject { }
public abstract class StorageObject : HWObject { }
public class Server : NamedObject
{
[JsonConstructor]
internal Server() { }
public String CompanyName { get; set; }
public Boolean IsRunning { get; set; }
public CPUObject CPU { get; set; }
public MemoryObject Memory { get; set; }
public StorageObject Storage { get; set; }
}
我有以下 DBContext:
public class DataContext : DbContext
{
public DataContext() : base("name=DataContext") { }
public DbSet<Server> Servers { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
}
}
当我尝试查看只读数据模型时出现以下错误:
System.InvalidOperationException: The abstract type 'MyApp.CPUObject' has no mapped descendants and so cannot be mapped. Either remove 'MyApp.CPUObject' from the model or add one or more types deriving from 'MyApp.CPUObject' to the model.
如何更改 DBContext 以使错误消失?
编辑: 根据下面的回答,我已经从 CPUObject、MemoryObject 和 StorageObject 中删除了抽象,因为它们本不应该是抽象的。
我已将 DBContext 更改为以下内容:(映射创建一对一的关系)
public class DataContext : DbContext
{
public DataContext() : base("name=DataContext") { }
public DbSet<Server> Servers { get; set; }
public DbSet<CPUObject> CPUObjects { get; set; }
public DbSet<MemoryObject> MemoryObjects { get; set; }
public DbSet<StorageObject> StorageObjects { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Server>().HasRequired(u => u.CPU).WithRequiredDependent().Map(m => m.MapKey("CPUID"));
modelBuilder.Entity<Server>().HasRequired(u => u.Memory).WithRequiredDependent().Map(m => m.MapKey("MemoryID"));
modelBuilder.Entity<Server>().HasRequired(u => u.Storage).WithRequiredDependent().Map(m => m.MapKey("StorageID"));
}
}
CPUObject
目前是一个抽象class,无法实例化。只需删除 abstract
即可使其成为具体对象。然后,将 public DbSet<CPUObject> CPUs { get; set; }
添加到您的 DBContext
您当前收到错误是因为 EF 正在寻找一个具体的 class,因此该错误表明您可以创建另一个继承(即 'deriving from')CPUObject[的具体 class =14=]