简体   繁体   English

MVC是否可以使用相同的linq to sql dbml从具有相同模式的多个数据库显示强类型视图?

[英]Can a MVC strongly typed view display data from multiple databases with identical schemas using the same linq to sql dbml?

I built a starter MVC app using strongly typed views to display table data from two identical databases in the same view (Index/list) by changing the connection string of the linq to sql dbml to switch between the two dbs and in debug mode I can see the change has taken place, but the view does not change to reflect the new data. 我通过使用linq到sql dbml的连接字符串来在两个dbs之间切换并使用调试模式,使用强类型视图构建了一个初学者MVC应用,以在同一视图(索引/列表)中显示来自两个相同数据库的表数据。看到发生了更改,但是视图没有更改以反映新数据。 I followed the steps from nerddinner tutorial and used the interface/repository for retrieving data. 我按照nerddinner教程中的步骤进行操作,并使用接口/存储库来检索数据。 Is this possible in an MVC application using strongly typed views and linq to sql dbml for the model? 在使用强类型视图和linq to sql dbml作为模型的MVC应用程序中,这可能吗?

You should be able to create a ViewModel, bind the view to the ViewModel, and simply select the data into an instance of your ViewModel. 您应该能够创建ViewModel,将视图绑定到ViewModel,然后只需将数据选择到ViewModel的实例中即可。 If you do it this way, it won't matter whether the data comes from a db, an XML file, or constants. 如果以这种方式进行操作,则数据是来自db,XML文件还是常量都无关紧要。 See how this blog post puts LINQ results into strongly-typed objects (you'd do this into your ViewModel). 查看此博客文章如何将LINQ结果放入强类型对象中(您可以在ViewModel中完成此操作)。

Here's an example: 这是一个例子:

    IEnumerable<MyViewModel> q = from c in Customers
            where c.firstname == "John"
            select new MyViewModel(c.company, c.lastname);                      

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

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