简体   繁体   English

多个表中的相同模型

[英]Same model in more than one table

I have a class, suppose it's called EntityModel , and I want to make three different tables with the same columns, as defined in EntityModel . 我有一个类,假设它叫EntityModel ,我想用EntityModel定义的三个具有相同列的不同表。 Let's call the tables tbPast , tbPresent and tbFuture . 我们将这些表tbPasttbPresenttbFuture I want also to access them separetely in the Entity DbContext : 我也想在Entity DbContext访问它们:

using (var db = new MyContext())
{
    var element = db.Past.Find(id);
    db.Past.Remove(element);
    db.Present.Add(element);
    db.SaveChanges();
}

The main purpose of having three tables is performance: the table will have millions of rows, and the most important is the Present , with dozens of rows. 具有三个表的主要目的是性能:该表将具有数百万行,而最重要的是Present ,具有数十行。 Most queries will be made in the Present table. 大多数查询将在Present表中进行。

What is the best way to do this? 做这个的最好方式是什么? Implementing three models with the same properties doesn't seem right for me. 对我来说,实现具有相同属性的三个模型似乎不合适。

I'm using Entity Framework, with the Code First approach, along with ASP.NET MVC 3. 我正在使用带有“代码优先”方法的Entity Framework和ASP.NET MVC 3。

You can't use the same model to generate separate tables w/ EF code-first. 您不能使用同一模型通过EF代码优先生成单独的表。 If you need to have some sort of grouping, use a Discriminator field and assing it any of the values: Past Present Future . 如果您需要进行某种分组,请使用Discriminator字段并将其赋值为以下任何值: Past Present Future

Edit: 编辑:

Similar effect can be achieved through table-per-concrete type inheritance. 通过每具体表类型继承可以达到类似的效果。 Thus each type will have it's own table and can share most (if not all) of the fields. 因此,每种类型都有自己的表,并且可以共享大多数(如果不是全部)字段。

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

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