简体   繁体   English

实体框架和抽象类

[英]Entity Framework and abstract class

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 '.我有一个名为“ Monitor ”的类,其中包含一个“ MonitorField ”列表。

Each ' MonitorField ' have a List of an abstract class called 'AMonitoringTool'**每个“ MonitorField ”都有一个名为“AMonitoringTool”的抽象类列表**

AMonitoringTool is provide to allow another developer to create his own kind of field, by inheriting from AMonitoringTool in an external DLL. AMonitoringTool允许其他开发人员通过从外部 DLL 中的AMonitoringTool继承来创建他自己的类型的字段。

The main issue is that the application do not know the real type in ' MonitorField ', preventing from saving my objects in database.主要问题是应用程序不知道“ MonitorField ”中的真实类型,从而阻止将我的对象保存在数据库中。

I have a MonitorEntity with a DbSet , but I can't save my Monitor list, I get this error message :我有一个带有 DbSetMonitorEntity ,但我无法保存我的 Monitor 列表,我收到以下错误消息:

"The abstract type '{...}. AMonitoringTool ' has no mapped descendant and so cannot be mapped..." “抽象类型'{...}。 AMonitoringTool '没有映射的后代,因此无法映射......”

My first thought was to implement the mapping in every DLL that inherit from ' AMonitoringTool ', but I don't how to do it.我的第一个想法是在从“ AMonitoringTool ”继承的每个 DLL 中实现映射,但我不知道如何去做。

MonitorEntity.cs监控实体.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 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.您似乎希望该库的使用者拥有自己的AMonitoringTool实现。 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:这样的事情应该工作:

//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; }

}

So now if a consumer wants a context, they create their own class:所以现在如果消费者想要一个上下文,他们会创建自己的类:

public MyMonitoringTool : IMonitoringTool
{
    public string ForcedProperty { get; set; }
    public string MyCustomProperty { get; set; }
}

And create their own context:并创建自己的上下文:

var myContext = new MonitorEntity<MyMonitoringTool>();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM