简体   繁体   English

脚手架-访问不同数据库中表的字段

[英]Scaffolding - access fields from a table in a different database

I have a scaffolded C# site showing a table in one database, but I would like to expose an associated field from a table in another database. 我有一个脚手架的C#站点,它在一个数据库中显示一个表,但是我想从另一个数据库中的表中公开一个相关字段。

There is a stored procedure in the current database that retrieves items from the secondary table, but how do I encapsulate this data and use it? 当前数据库中有一个存储过程可以从辅助表中检索项目,但是如何封装这些数据并使用它呢?

I have added the stored procedure to the current model, but I don't see a way of making use of it. 我已经将存储过程添加到当前模型中,但是我看不到任何使用它的方法。

I have looked into Complex Types, thinking that this may be the route to take, but I can't seem to find the documentation I'm looking for. 我研究了复杂类型,认为这可能是解决问题的方法,但是我似乎找不到所需的文档。

You need to make sure that you can access both databases from the datacontext that your controller is using (I'm guessing that you're using MVC). 您需要确保可以从控制器使用的数据上下文访问两个数据库(我猜您正在使用MVC)。 If you cannot access the second database from your current context. 如果无法从当前上下文访问第二个数据库。 Make sure to access a context that can, or add that database to a new connection string and access it separately. 确保访问可以的上下文,或者将该数据库添加到新的连接字符串中并分别访问它。

Check in the specific controller to make sure it's giving access to that field in the correct database and table. 检入特定的控制器,以确保它允许访问正确的数据库和表中的该字段。 Once that's done, you will need to recompile (build) and update the view by accessing it through the model syntax: 完成后,您将需要通过模型语法访问视图以重新编译(构建)并更新视图:
@model.yourNewField @ model.yourNewField

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

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