C# linq to sql - 动态选择表

发布于 2024-12-01 08:43:43 字数 519 浏览 1 评论 0原文

我有以下场景:有一个数据库每年都会生成一个新的 logTable。它于2001年开始营业,目前拥有11张桌子。它们都具有相同的结构,因此具有相同的字段、索引、pk 等。

我有一些称为管理器的类,顾名思义,它们管理该数据库上的每个操作。对于每个不同的表,我都有一个经理,除了这个 logTable,我只有一个经理。

我读了很多书并尝试了不同的方法,例如使用 ITable 动态获取表或我的所有表都实现的接口。不幸的是,我丢失了强类型属性,因此我无法进行任何搜索或更新或任何操作,因为我无法使用 logTable.Where(q=> q.ID == paramId)

考虑到这些表具有相同的结构,搜索 2010 年以来的日志的查询可能与搜索 2011 年及之后的日志的查询完全相同。

我问这个只是因为我不想为每个表重写相同的代码,因为它们在结构上是相等的。

编辑

我使用 Linq to SQL 作为我的 ORM。而且这些表使用了所有数据库操作,而不仅仅是选择。

I have the following scenario: there are a database that generates a new logTable every year. It started on 2001 and now has 11 tables. They all have the same structure, thus the same fields, indexes,pk's, etc.

I have some classes called managers that - as the name says - manages every operation on this DB. For each different table i have a manager, except for this logTable which i have only one manager.

I've read a lot and tried different things like using ITable to get tables dynamically or an interface that all my tables implements. Unfortunately, i lose strong-typed properties and with that i can't do any searches or updates or anything, since i can't use logTable.Where(q=> q.ID == paramId).

Considering that those tables have the same structure, a query that searches logs from 2010 can be the exact one that searches logs from 2011 and on.

I'm only asking this because i wouldn't like to rewrite the same code for each table, since they are equal on it's structure.

EDIT

I'm using Linq to SQL as my ORM. And these tables uses all DB operations, not just select.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

情深已缘浅 2024-12-08 08:43:43

考虑将所有日志放在一张表中并使用 分区 以保持性能。如果这不可行,您可以创建一个将所有日志表合并在一起的视图,并在选择日志数据时使用该视图。这样,当您添加新的日志表时,您只需更新视图以包含新表。

编辑根据最近的评论:

听起来你需要一个新的 DBA,如果他不允许你创建新的 SP。是的,我认为可以定义一个 ILogTable 接口,然后让您的日志表类实现它,但这不允许您执行 GetTable() 操作。您必须拥有某种带有创建联合查询的方法的 DAL 类,例如

public IEnumerable<ILogTable> GetLogs()
{
    var Log2010 = from log in DBContext.2010Logs
                  select (ILogTable)log;
    var Log2011 = from log in DBContext.2011Logs
                  select (ILogTable)log;
    return Log2010.Concat(Log2011);
}

上面的代码完全未经测试,可能会严重失败;-)

编辑以使 @AS-CII 高兴;-)

Consider putting all your logs in one table and using partitioning to maintain performance. If that is not feasible you could create a view that unions all the log tables together and use that when selecting log data. That way when you added a new log table you just update the view to include the new table.

EDIT Further to the most recent comment:

Sounds like you need a new DBA if he won't let you create new SPs. Yes I think could define an ILogTable interface and then make your log table classes implement it, but that would not allow you do GetTable<ILogTable>(). You would have to have some kind of DAL class with a method that created a union query, e.g.

public IEnumerable<ILogTable> GetLogs()
{
    var Log2010 = from log in DBContext.2010Logs
                  select (ILogTable)log;
    var Log2011 = from log in DBContext.2011Logs
                  select (ILogTable)log;
    return Log2010.Concat(Log2011);
}

Above code is completely untested and may fail horribly ;-)

Edited to keep @AS-CII happy ;-)

氛圍 2024-12-08 08:43:43

您可能需要查看 Codeplex Fluent Linq to SQL 项目。我从未使用过它,但我熟悉在 EF4 中使用类似映射技术的想法。您可以创建单个对象并使用以下语法将其动态映射到不同的表:

public class LogMapping : Mapping<Log> {
    public LogMapping(int year) {
        Named("Logs" + year);
        //Column mappings...
    }
}

You might want to look into the Codeplex Fluent Linq to SQL project. I've never used it, but I'm familiar with the ideas from using similar mapping techniques in EF4. YOu could create a single object and map it dynamically to different tables using syntax such as:

public class LogMapping : Mapping<Log> {
    public LogMapping(int year) {
        Named("Logs" + year);
        //Column mappings...
    }
}
澜川若宁 2024-12-08 08:43:43

只要每个查询返回相同的形状,您就可以使用 ExecuteQuery("Select cols From LogTable" + instance)。请注意,ExecuteQuery 是 LINQ to SQL 允许 SQL 注入的一种情况。我在 http://www.thinqlinq.com/Post.aspx/Title/Does-LINQ-to-SQL-eliminate-the-possibility-of-SQL-Injection

As long as each of your queries return the same shape, you can use ExecuteQuery<Log>("Select cols From LogTable" + instance). Just be aware that ExecuteQuery is one case where LINQ to SQL allows for SQL Injection. I discuss how to parameterize ExecuteQuery at http://www.thinqlinq.com/Post.aspx/Title/Does-LINQ-to-SQL-eliminate-the-possibility-of-SQL-Injection.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文