简体   繁体   English

将多个实体映射到EF4抽象基类或接口?

[英]Map multiple entities to EF4 abstract base class or interface?

I have multiple tables in a db that are identical in terms of column structure. 我在数据库中有多个表,它们的列结构相同。

I'd like to have this modelled in EF4 so that they all inherit from a single abstract base class, so I can write common methods etc. 我希望在EF4中对此模型进行建模,以便它们都继承自单个抽象基类,因此我可以编写通用方法等。

I've tried an abstract base class, but this means that I need to add a property to each entity that is NOT in the base class. 我已经尝试了一个抽象基类,但这意味着我需要向不在基类中的每个实体添加一个属性。 It also appears I lose my collections, as each one is now a collection of the base type? 似乎我也失去了我的收藏,因为现在每个收藏都是基本类型的收藏?

I can add partial classes and inherit each from a common interface? 我可以添加部分类并从公共接口继承每个类吗?

eg 例如

Table 1     Table 2     Table 3
Id          Id          Id
Month1      Month1      Month1
Month2      Month2      Month2
Month3      Month3      Month3
Quarter2    Quarter2    Quarter2
Quarter3    Quarter3    Quarter3
Quarter4    Quarter4    Quarter4

How can I have a base class or interface called "table" so that I can write all my methods against that and not against each individual table? 我如何有一个称为“表”的基类或接口,以便我可以针对该表而不是针对每个单独的表编写所有方法?

If you are force to keep separate tables, and you just want to be able to write common methods, you can use an Interface and Extension methods like below: 如果您被迫保留单独的表,并且只想能够编写通用方法,则可以使用如下所示的Interface和Extension方法:

public interface ITableBase{
    int Id{ get; set; }
    // other properties
    void Method1();
    int Method2();
    string Method3(int some_arguments);
}

public partial class Table1 : ITableBase{
    // implement ITableBase and other properties and methods
}

public partial class Table2 : ITableBase{
    // implement ITableBase and other properties and methods
}

public partial class Table2 : ITableBase{
    // implement ITableBase and other properties and methods
}

static public class ITableBaseExtensions{
    static public string GetIdAsString(this ITableBase table){
        return table.Id.ToString();
    }
    static public int UsingMethod2(this ITableBase table){
        var i = table.Method2();
        return i * 5 + 9 - 14 / 3 % 7 /* etc... */;
    }
    static public void AddingNewMethodToTables(this ITableBase table, string some_string,
        int some_int, YourType any_other_parameter /* etc... */ ){
        // implement the method you want add to all Table classes
    }
    // 
    // You can add any method you want to add to table classes, here, as an Extension method
    //
}

And in consumers , you can call all methods defined in ITableBaseExtensions class for all tables: 在使用者中 ,您可以为所有表调用ITableBaseExtensions类中定义的所有方法:

public class MyConsumer{
    public void MyMethod(){
        Table1 t1 = new Table1();
        Table1 t2 = new Table2();
        Table1 t3 = new Table3();

        t1.UsingMethod2(); // will be called from ITableBaseExtensions
        t1.Method2(); // will be called from ITableBase - must implemented in Table1 class

        t2.Method3(some_argument); //  will be called from ITableBase - must implemented in Table2 class
        t2.GetIdAsString(); // will be called from ITableBaseExtensions

        // etc.
    }
}

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

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