Thursday 24 January 2013

Can't create Database. Permission denied on master database.

Recently I was working  on ASP.NET MVC 4.0 Project using Visual studio 2012 and trying entity framework code first. Entity framework Code First approach automatically creates the  database schema and tables based on model defined in the code.
After doing build and running my sample project I was getting an exception with the message.
"Can't create Database. Permission denied on master database!". 
I was surprised as I was administrator on my laptop and it took me some time to figure out what's happening. Finally I manage to resolve this issue.  Here is the detailed explanation
1.With Visual studio 2012 SQL-Server 2012 LocalDB Express is used for development.
2.On my laptop I had one more instance of SQL-Server express.  Entity framework was looking for this instance and I didn't  have sysadmin rights on this instance and hence was giving the issue. Also I had forgotten password for sa user and couldn't give rights to my account.
3. So I stopped this instance and disabled this service. So that I can use LocalDB express.
4. Last thing was telling the entity framework to check for LocalDB and I found the setting to be done in web.config. The setting is given below.

.

<entityFramework>
    <defaultConnectionFactory
        type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, 
            EntityFramework">
      <parameters>
        <parameter value="v11.0" />
      </parameters>
    </defaultConnectionFactory>
  </entityFramework>
5. I was then ready to recompile and test and it worked fine.
I hope you will find this useful! as I spent couple of hours to investigate what's happening.