简体   繁体   中英

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. I followed the steps from nerddinner tutorial and used the interface/repository for retrieving data. Is this possible in an MVC application using strongly typed views and linq to sql dbml for the model?

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. If you do it this way, it won't matter whether the data comes from a db, an XML file, or constants. See how this blog post puts LINQ results into strongly-typed objects (you'd do this into your ViewModel).

Here's an example:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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