Posts

Showing posts from May, 2012

Troubleshooting Database Connectivity Issues When Unit Testing With MSTest

I was attempting to run some unit tests today for an ASP.NET MVC project I'm working on when I ran into the following fun little exception: Test method BlogMVC.Tests.Controllers.HomeControllerTest.Index threw exception: System.Data.EntityException: The underlying provider failed on Open. ---> System.Data.SqlClient.SqlException: An attempt to attach an auto-named database for file C:\Users\Alan\Documents\Visual Studio 2010\Projects\BlogMVC\TestResults\Alan_QUIGON 2012-05-15 16_54_57\Out\BlogMVC.mdf failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share. The issue is that each unit test is run in it's own individual directory, and in my case the unit tests needed my database files, which were not being deployed to the unit test directory. Fortunately this is easy to correct. To fix this issue, do the following: Right-click on the solution (not the project) and select "Add -> New Item". Choose "

Using a View From a Different Controller in ASP.NET MVC

I'm currently working on a pet project in ASP.NET MVC: an MVC blog engine. I don't plan on using it to host my blog, but rather as an exercise in ASP.NET MVC. In my HomeController class, I want to retrieve the ten most recent blog posts and render them using the Index view for the BlogPost controller. This way, when a user navigates to the default page of the blog, they'll see a list of the most recent entires. But how could I use a BlogPost view (that isn't part of the Shared views folder) from within the HomeController class? To achieve this, I did the following in the Index() method of my HomeController class -- notice the line in bold (this is in MVC 3): public   ActionResult  Index() {     ViewBag.Message =  "My MVC-Powered Blog!" ;     BlogRepository  repo =  new   BlogRepository ();     BlogRepository . BlogPostCriteria  criteria =          new   BlogRepository . BlogPostCriteria  { NumberToRetrieve = 10 };                  var  blogPosts = repo

Finding Tables in SQL Server That Contain a Certain Column Name

Here's a query you can run in SQL Server to find tables containing a certain column name -- just replace "UserId" in the WHERE clause below with the text of the column name you're searching for: SELECT    t.name AS TableName,         SCHEMA_NAME(schema_id) AS SchemaName,         c.name AS ColumnName FROM    sys.tables AS t INNER JOIN sys.columns c ON t.OBJECT_ID = c.OBJECT_ID WHERE    c.name LIKE '%UserId%' -- Replace UserId with what you're looking for ORDER BY SchemaName, TableName;