简体   繁体   English

实体框架:如何为同一个实体使用多个表?

[英]Entity Framework: how to use multple tables for the same entity?

My situation is as follows. 我的情况如下。 I'm using entity framework (4.0) in which I have defined a relatively simple model like, let's say, two entities: 我正在使用实体框架(4.0),我在其中定义了一个相对简单的模型,比如两个实体:

  • user 用户
  • transaction 交易

A user can have multiple transactions in this scenario. 在这种情况下,用户可以拥有多个事务。 When generating the database this would result (obviously) in 2 database tables names 'user' and 'transaction'. 生成数据库时,这显然会导致2个数据库表中的“user”和“transaction”。

The problem is that I want to use the code as a webservice where different companies should have their own environment (read: own users and transactions). 问题是我想将代码用作Web服务,其中不同的公司应该拥有自己的环境(读取:自己的用户和事务)。 A simple solution would be to add a column to both tables like, lets say 'company_id' to identify one user/transactions for companya from the user/transaction from companyb. 一个简单的解决方案是向两个表添加一个列,例如,'company_id'用于从companyb的用户/事务中识别companya的一个用户/事务。

What I would like to have is a duplication of the tables like so: 我想要的是重复表格如下:

  • compa_user compa_user
  • compa_transaction compa_transaction
  • compb_user compb_user
  • compb_transaction compb_transaction

This way..all data would be nicely separated and if company a generates a lot of transactions, company b would not notice that the system is getting slow or whatsoever. 通过这种方式,所有数据都可以很好地分开,如果公司a生成大量交易,公司b就不会注意到系统变慢或者无论如何。

My question: is there a way to accomplish this based on entity framework. 我的问题:有没有办法在实体框架的基础上实现这一目标。 So, can I have multiple tables representing one entity in my model and switch from table to table depending on which company is connecting to the service. 那么,我可以在模型中有多个表代表一个实体,并根据哪个公司连接到服务从一个表切换到另一个表。

Any help appreciated! 任何帮助赞赏!

If you really want to keep the tables seperate, then a seperate database for each client would be the easiest - only need to change the connection string in EF. 如果你真的想保持表分开,那么每个客户端的单独数据库将是最简单的 - 只需要更改EF中的连接字符串。

The other benefit of this model (seperate databases) is that the database will scale quite easily as each database could theoretically be on a different database server should the DB ever become the bottleneck. 该模型(单独的数据库)的另一个好处是数据库可以非常容易地扩展,因为如果数据库成为瓶颈,理论上每个数据库可以在不同的数据库服务器上。

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

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