sol3(dot)net

Home of Barrows Software Solutions, LLC

  • Home
  • Blog
  • Articles
  • Links
  • Notes
  • About
Blog

Entity Frameworks 4.1 on SQL Server–some gotchas

I am using MVC3, Visual Studio 2010 SP1 & Entity Framework 4.1.

I was very excited to get my hands dirty with the Code First piece of the new Entity Frameworks.  There are some great articles out there for getting started and a couple of my favorites are:

  • http://thedatafarm.com/blog/data-access/new-ef4-amp-ef4-1-content-on-msdn/ by Julie Lerman
  • http://weblogs.asp.net/scottgu/archive/2011/03/19/rc-of-entity-framework-4-1-which-includes-ef-code-first.aspx by Scott Guthrie
  • http://weblogs.asp.net/scottgu/archive/2010/07/16/code-first-development-with-entity-framework-4.aspx by Scott Guthrie (based on a CTP so some things are still changing)

Armed with the above information I started right in on my first Code First project, more to learn how to rather than create something lasting.  I decided to target a “real” SQL Server as I already had SQL 2005 on my dev machine.  I am also keeping in mind that I want to rewrite my own site (http://sol3.net) and that runs on a SQL 2008 shared hosting server.  So, I went into SQL and created a SQL Server based Login with the right privileges.  Now remember, on a shared hosting plan what ever SQL Login you are using will not have rights to drop the database which appears to be one of the things Code First will do when it detects a change. 

Needless to say, I have not got it working with a SQL Login locally so opted to switch to a Trusted Login based on my NT account.  That worked like a charm – the first time I ran the code.  Database built, objects in place, etc.  The next step was seeding some data each time a new build is executed.  (See http://thedatafarm.com/blog/data-access/seeding-a-database-with-ef4-code-first/ for how to do this.)  Now we start in on the gotchas!

1) While doing this I had SQL Manager open so I could execute some quick queries to see the data after creation.  When I ran my project I started getting some weird errors and spent about 2 hours tweaking my seed code to figure out what was causing it.  Then I had one of those <say “Doh!” voice=”Homer SImpson” /> moments.

  • If you have a SQL window open on the database then Code First bombs as the DB is locked and can’t be dropped.
  • Even if you close the window but the SPID is still spun up in SQL you will get errors.
  • If you have the DB (or any part) actively selected in the tree within SQL Mangler, again, you get errors.

So please, avoid my pain and check your database to see if it is locked!  Code First does not work on a locked database.  It has to be able to drop the DB and recreate it (as I understand it so far).


URL Date: 2011-06-02;
Full Url: /2011/06/02/
Created: 6/2/2011 12:19 PM
CreatedBy: keithb
Updated:
UpdatedBy:

Edit | Back to List




Some links of interest (varied)

 

  • Entity Framework FAQ (new)

This is a collection of frequently asked questions (and answers) about the Entity Framework (a .Net-based Object-Relational Mapping framework from Microsoft).

 

mono-android[1]
  • Mono for Android (blog entry)
  • Mono for Android (install)
  • MonoDroid (home page)

MonoDroid is a development stack for using C# and core .NET APIs to develop Android-based applications. MonoDroid will be a commercial product licensed in a similar fashion to our Mono for iPhone product MonoTouch.

 

 

 

 beer_reasonably_small[1]

 

  •  Open Craft Beer (blog)

This is an open blog for all lovers and purveyors of great craft beers.


URL Date: 2011-06-02;
Full Url: /2011/06/02/
Created: 6/2/2011 12:19 PM
CreatedBy: keithb
Updated:
UpdatedBy:

Edit | Back to List




Contact



Tags
ASP.NETAuthorizationC#Entity FrameworkGardeningJava ScriptLearningLINQMVCNotesSQL ServerVisual StudioWeb Design

About Me

ASPInsiders' Website

News Letter (ASPInsiders)

News Letter (Sol3)

LinkedIn

My Resume

My Portfolio

Recent Posts

Some links of interest (varied)

Entity Frameworks 4.1 on SQL Server–some gotchas

ASP.NET Team Blogs

Scott Guthrie

Scott Hanselman

Phil Haack

G. Andrew Duthie

Mads Kristensen

ASPInsiders

Luis Abreu

Jason Alexander

Mike Amundsen

Rachel Appel

Adrian Hope-Bailie

Tim Barcz

David Barkol

Keith Barrows

Michele Bustamante

Michael Campbell

Scott Cate

Robert Chartier

Simone Chiaretta

Dana Coffey

Jonathan Cogley

Mitch Denny

Brian Desmond

Scott Dockendorf

Brendan Enrick

Scott Forsyth

Chris Frazier

Alessandro Gallo

Jon Galloway

Jason N. Gaylord

Barry Gervin

Paul Glavich

Jonathan Goodyear

Alex Homer

Thomas Johansen

Anil John

Marcie Jones

Bipin Joshi

Teemu Keiski

Peter Kellner

Colt Kwong

Julia Lerman

J. Ambrose Little

Paul Litwin

Wallace McClure

Galen Earl Murdock

Michael Neel

Darren Neimke

Fredrik Normen

Ryan Olshan

Michael Palermo

Kris van der Mast

David Penton

Steve Presley

Rachel Reese

Eli Robillard

Ben Scheirman

Michael Schwarz

James Shaw

Craig Shoemaker

Eric J. Smith

Steven Smith

Rick Strahl

Dave Sussman

Ben Tiedt

Dan Wahlin

Mike Warriner

Scott Watermasysk

Derik Whittaker

Jeff Widmer

David Wier

Christoph Wille

David Yack

Archives

(Content coming soon)

Copyright © 2011 - Barrows Software Solutions, LLC - dba: Blue Skies Software - All rights reserved.