使用来自2个不同数据库的表 - 实体框架

时间:2021-08-08 16:48:19

I have a model got from a sql 2008 database, now i need, in the same model file to create a entity that represent a user-base from another database. How I can do this in the same model and avoid creating a database view.

我有一个来自sql 2008数据库的模型,现在我需要,在同一模型文件中创建一个代表另一个数据库的用户库的实体。我如何在同一模型中执行此操作,并避免创建数据库视图。

1 个解决方案

#1


You could create a VIEW in the SSDL. I.e. not a database view but an Entity Framework view.

您可以在SSDL中创建VIEW。即不是数据库视图,而是实体框架视图。

Take a look at the documentation for DefiningQuery. DefiningQueries have a native SQL body, just like a database View, but you don't need a view in the database.

看一下DefiningQuery的文档。 DefiningQueries具有本机SQL主体,就像数据库视图一样,但您不需要数据库中的视图。

At this point the Entity becomes readonly so you need to add Functions (to the SSDL) and ModificationFunction Mappings (to the MSL) for to make the Entity Read-Write again.

此时,实体变为只读,因此您需要添加函数(到SSDL)和ModificationFunction映射(到MSL)以再次进行实体读写。

Note once the Functions are in the SSDL you should use the designer to create the modifications mappings rather than mucking around in the XML.

注意,一旦函数在SSDL中,您应该使用设计器来创建修改映射,而不是在XML中进行修改。

Generally Functions in the SSDL are simply wrappers around stored procs, but seeing as you don't want to add a database view you probably don't want to create stored procs either.

通常,SSDL中的函数只是存储过程的包装器,但是当您不想添加数据库视图时,您可能也不想创建存储过程。

Luckily you can embed the CommandText for the SSDL functions directly in the SSDL too (i.e. native SQL again).

幸运的是,您也可以直接在SSDL中嵌入用于SSDL函数的CommandText(即再次使用本机SQL)。

If you follow all these steps you can get what you want.

如果您按照所有这些步骤操作,就可以得到您想要的结果。

But I admit it is NOT easy.

但我承认这并不容易。

Hope this helps (at least by giving you some hints on how to get started).

希望这有所帮助(至少通过给你一些关于如何开始的提示)。

Alex (Entity Framework Team)

Alex(实体框架团队)

#1


You could create a VIEW in the SSDL. I.e. not a database view but an Entity Framework view.

您可以在SSDL中创建VIEW。即不是数据库视图,而是实体框架视图。

Take a look at the documentation for DefiningQuery. DefiningQueries have a native SQL body, just like a database View, but you don't need a view in the database.

看一下DefiningQuery的文档。 DefiningQueries具有本机SQL主体,就像数据库视图一样,但您不需要数据库中的视图。

At this point the Entity becomes readonly so you need to add Functions (to the SSDL) and ModificationFunction Mappings (to the MSL) for to make the Entity Read-Write again.

此时,实体变为只读,因此您需要添加函数(到SSDL)和ModificationFunction映射(到MSL)以再次进行实体读写。

Note once the Functions are in the SSDL you should use the designer to create the modifications mappings rather than mucking around in the XML.

注意,一旦函数在SSDL中,您应该使用设计器来创建修改映射,而不是在XML中进行修改。

Generally Functions in the SSDL are simply wrappers around stored procs, but seeing as you don't want to add a database view you probably don't want to create stored procs either.

通常,SSDL中的函数只是存储过程的包装器,但是当您不想添加数据库视图时,您可能也不想创建存储过程。

Luckily you can embed the CommandText for the SSDL functions directly in the SSDL too (i.e. native SQL again).

幸运的是,您也可以直接在SSDL中嵌入用于SSDL函数的CommandText(即再次使用本机SQL)。

If you follow all these steps you can get what you want.

如果您按照所有这些步骤操作,就可以得到您想要的结果。

But I admit it is NOT easy.

但我承认这并不容易。

Hope this helps (at least by giving you some hints on how to get started).

希望这有所帮助(至少通过给你一些关于如何开始的提示)。

Alex (Entity Framework Team)

Alex(实体框架团队)