简体   繁体   English

生成的Entity Framework类可以跨多个数据库使用吗?

[英]Can the generated Entity Framework classes be used across multiple databases?

So I have multiple copies of the same database. 所以我有同一个数据库的多个副本。 Some of the tables are slightly different, but the ones I'm generating EF entities against are all identical. 有些表略有不同,但是我针对它们生成EF实体的表完全相同。 Does EF4 support making database calls against different databases just by swapping out the connection string or is it coupled somehow to the database it was generated against? EF4是否仅通过换出连接字符串就支持对不同数据库进行数据库调用,或者它是否以某种方式耦合到了它所针对的数据库?

You shouldn't have any pain by simply swapping the connectionstring assuming the databases are really identical. 假设数据库确实相同,只需交换连接字符串就不会有任何麻烦。 See it this way, the .edmx consists out of three parts: 以这种方式查看,.edmx由三部分组成:

  1. Entity definition 实体定义
  2. Database definition and 数据库定义和
  3. Mapping between these two. 两者之间的映射。

Whether you use the entity model against db1 or db2 doesn't matter at all as long as one of those db's was used to generate the entity model and "db1=db2". 只要使用其中一个db来生成实体模型和“ db1 = db2”,对db1还是db2使用实体模型都没有关系。 The Sql that EF generates under the hood to communicate with the database is based entirely on the .edmx. EF在幕后生成的与数据库通信的Sql完全基于.edmx。 So as long as your databases are identical expect no problems, they can be "reached" based upn the same .edmx! 因此,只要您的数据库相同就不会出现问题,就可以基于相同的.edmx来“访问”它们! Try it yourself! 自己尝试!

If you are really interested in EF and planning to use it, this (Programming Entity Framework by Julie Lerman) is a must read 如果您真的对EF感兴趣并计划使用它,则必须阅读(Julie Lerman的《编程实体框架》)

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

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