Entity Framework 和摘要 class
Entity Framework and abstract class
我是 Entity Framework 的新手,想知道我想做的事情是否可行。
我有一个名为“Monitor”的 class,其中包含一个列表“MonitorField”。
每个“MonitorField”都有一个名为 'AMonitoringTool'**
的抽象列表 class
AMonitoringTool 允许其他开发人员通过在外部 DLL 中继承 AMonitoringTool 来创建自己的字段类型。
主要问题是应用程序不知道“MonitorField”中的真实类型,因此无法将我的对象保存在数据库中。
我有一个 MonitorEntity 和一个 DbSet,但我无法保存我的监视器列表,我收到此错误消息:
"The abstract type '{...}.AMonitoringTool' has no mapped descendant and so cannot be mapped..."
我的第一个想法是在每个继承自“AMonitoringTool”的 DLL 中实现映射,但我不知道该怎么做。
MonitorEntity.cs
public class MonitorEntity : DbContext
{
public DbSet<Monitor> Monitors { get; set; }
public MonitorEntity()
{
}
}
Monitor.cs
public class Monitor
{
public Monitor(string name)
{
MonitorName = name;
FieldList = new List<MonitorField>();
}
private List<MonitorField> m_fieldList = null;
public virtual List<MonitorField> FieldList
{
get
{
return m_fieldList;
}
set
{
m_fieldList = value;
}
}
}
MonitorField.cs
public class MonitorField
{
public AMonitoringTool Configuration { get; set; }
public MonitorField()
{
FieldName = "<label>";
}
}
您似乎希望此库的使用者拥有自己对 AMonitoringTool
的实现。我建议您使用通用类型参数创建上下文,让消费者决定它是什么。这样的事情应该有效:
//This isn't strictly needed but it will let you force some
//Specific fields for the monitoring tool if you like
public interface IMonitoringTool
{
string ForcedProperty { get; set; }
}
//Here the type parameter get used for the Configuration property:
public class MonitorField<T> where T : IMonitoringTool
{
public T Configuration { get; set; }
public string FieldName { get; set; }
public MonitorField()
{
FieldName = "<label>";
}
}
//And this is the context:
public class MonitorEntity<T> : DbContext where T : IMonitoringTool
{
public DbSet<Monitor<T>> Monitors { get; set; }
}
public class Monitor<T> where T : IMonitoringTool
{
public Monitor(string name)
{
MonitorName = name;
FieldList = new List<MonitorField<T>>();
}
public string MonitorName { get; set; }
public List<MonitorField<T>> FieldList { get; set; }
}
所以现在如果消费者想要上下文,他们会创建自己的上下文 class:
public MyMonitoringTool : IMonitoringTool
{
public string ForcedProperty { get; set; }
public string MyCustomProperty { get; set; }
}
并创建自己的上下文:
var myContext = new MonitorEntity<MyMonitoringTool>();
我是 Entity Framework 的新手,想知道我想做的事情是否可行。
我有一个名为“Monitor”的 class,其中包含一个列表“MonitorField”。
每个“MonitorField”都有一个名为 'AMonitoringTool'**
的抽象列表 classAMonitoringTool 允许其他开发人员通过在外部 DLL 中继承 AMonitoringTool 来创建自己的字段类型。
主要问题是应用程序不知道“MonitorField”中的真实类型,因此无法将我的对象保存在数据库中。
我有一个 MonitorEntity 和一个 DbSet,但我无法保存我的监视器列表,我收到此错误消息:
"The abstract type '{...}.AMonitoringTool' has no mapped descendant and so cannot be mapped..."
我的第一个想法是在每个继承自“AMonitoringTool”的 DLL 中实现映射,但我不知道该怎么做。
MonitorEntity.cs
public class MonitorEntity : DbContext
{
public DbSet<Monitor> Monitors { get; set; }
public MonitorEntity()
{
}
}
Monitor.cs
public class Monitor
{
public Monitor(string name)
{
MonitorName = name;
FieldList = new List<MonitorField>();
}
private List<MonitorField> m_fieldList = null;
public virtual List<MonitorField> FieldList
{
get
{
return m_fieldList;
}
set
{
m_fieldList = value;
}
}
}
MonitorField.cs
public class MonitorField
{
public AMonitoringTool Configuration { get; set; }
public MonitorField()
{
FieldName = "<label>";
}
}
您似乎希望此库的使用者拥有自己对 AMonitoringTool
的实现。我建议您使用通用类型参数创建上下文,让消费者决定它是什么。这样的事情应该有效:
//This isn't strictly needed but it will let you force some
//Specific fields for the monitoring tool if you like
public interface IMonitoringTool
{
string ForcedProperty { get; set; }
}
//Here the type parameter get used for the Configuration property:
public class MonitorField<T> where T : IMonitoringTool
{
public T Configuration { get; set; }
public string FieldName { get; set; }
public MonitorField()
{
FieldName = "<label>";
}
}
//And this is the context:
public class MonitorEntity<T> : DbContext where T : IMonitoringTool
{
public DbSet<Monitor<T>> Monitors { get; set; }
}
public class Monitor<T> where T : IMonitoringTool
{
public Monitor(string name)
{
MonitorName = name;
FieldList = new List<MonitorField<T>>();
}
public string MonitorName { get; set; }
public List<MonitorField<T>> FieldList { get; set; }
}
所以现在如果消费者想要上下文,他们会创建自己的上下文 class:
public MyMonitoringTool : IMonitoringTool
{
public string ForcedProperty { get; set; }
public string MyCustomProperty { get; set; }
}
并创建自己的上下文:
var myContext = new MonitorEntity<MyMonitoringTool>();