I'm new to Entity Framework and would like to know if, what I want to do, is possible.
I have a class named 'Monitor' which contains a List of 'MonitorField'.
Each 'MonitorField' have a List of an abstract class called 'AMonitoringTool'**
AMonitoringTool is provide to allow another developer to create his own kind of field, by inheriting from AMonitoringTool in an external DLL.
The main issue is that the application do not know the real type in 'MonitorField', preventing from saving my objects in database.
I have a MonitorEntity with a DbSet, but I can't save my Monitor list, I get this error message :
"The abstract type '{...}.AMonitoringTool' has no mapped descendant and so cannot be mapped..."
My first thought was to implement the mapping in every DLL that inherit from 'AMonitoringTool', but I don't how to do it.
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>";
}
}
You seem to want consumers of this library to have their own implementation of what a
AMonitoringTool
is. I would suggest you create your context with a generic type parameter to let the consumer decide what it is. Something like this should work:So now if a consumer wants a context, they create their own class:
And create their own context: