When Using EF CodeFirst With Visual Studio, How to Live With Cannot drop database because it is in use error.


Problem So, if you have been doing development with Visual Studio 2010, Entity Framework CodeFirst, SqlServer or SqlServerCE for any amount of time, you’ll quickly run into the problem that the database can not be reinitialized because it is open.  Basically, the scenario is this. 1)  Put in your Global.asax.cs file a line that always recreates the database (naturally because you are in a development mode and as you constantly change your model and seed data).  The line is something like this:  Database.SetInitializer(new DropCreateDatabaseAlways<SiteDB>()); 2)  Run your application with something like Debug/Run (All is fine) 3)  Go into the database browser (either in Visual Studio or Enterprise … Continue Reading

Tip for Using SQLServer To Count By Alpha Names in a list


  So, this is not a big tip, but worth at least 10 minutes to figure it out on your own.  Here is to saving you 10 minutes:   SELECT LEFT (userlastname, 1) as alpha, count(id)FROM attendeesWHERE id IN ( SELECT attendeesid FROM AttendeesCodeCampYear WHERE codecampyearid = 6 )GROUP BY LEFT (userlastname, 1)OORDER BY LEFT (userlastname, 1) It’s pretty self explanatory.  Our case is we have two tables that we track code camp attendees.  One is the master list, and the other is a detail by year.  (6 is this year)   … Continue Reading

My First Experience With SqlServer CE 4.0


I have heard all the hype about how great Sql Server CE 4.0 and that it is now standard with Visual Studio 2010 SP1.     I’ve got a small project (1 table) that I’d like to include in an asp.net website project so I decided to give it a try.  For the most part, things have gone smoothly, however I did have a couple hiccups I’d like to mention.  One cost me $9 because I was doing this implementation on an airplane and immediately, I had a failed connection with a confusing error that I needed Bing to lookup  (more details below).   … Continue Reading

Connecting To SqlServer From Web.Config Without a Password (Trusted Connection)

I often forget that the simplest way (IMHO) to connect to a Microsoft SqlServer 2008 database is to use the web.config connection for making a Trusted connection.  Basically, it keeps you from having to put a username and password in the web.config and also from having to keep track of different username and passwords on different systems (like where you deploy to for example).  The simplest connection string I can think of looks like this:     <add name="MyConnName" connectionString="Server=.;Database=mydbcatalogname;Trusted_Connection=True;" providerName="System.Data.SqlClient" /> Notice that I’m using the “.” as the server name.  This allows me to reference the … Continue Reading


Get every new post delivered to your Inbox

Join other followers: