locked
Database Error in MVC RRS feed

  • Question

  • User52625461 posted

    Hello All,

    While creating a database through code I am gettting below error

    CREATE DATABASE permission denied in database 'master' 

    Below is step by step execution of code

    Step 1; Global.asax code called

    protected void Application_Start()
            {
                AreaRegistration.RegisterAllAreas();
                IdentityConfig.RegisterIdentities();
                FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
                RouteConfig.RegisterRoutes(RouteTable.Routes);
                BundleConfig.RegisterBundles(BundleTable.Bundles);
            }

    Step 2.: It is calling  IdentityConfig.RegisterIdentities() code

     public static class IdentityConfig
        {
            public static void RegisterIdentities()
            {
                // Ensures the default demo user is available to login with
                UserManager.Seed();
            }
        }

    Step 3: 'Seed' method is getting called

    public class UserManager : UserManager<IdentityUser>
        {
            private static readonly UserStore<IdentityUser> UserStore = new UserStore<IdentityUser>();
            private static readonly UserManager Instance = new UserManager();
           
            public UserManager()
                : base(UserStore)
            {
            }
    
    public static void Seed()
            {
                // Make sure we always have at least the demo user available to login with
                // this ensures the user does not have to explicitly register upon first use
                var demo = new IdentityUser
                {
                    Id = "6bc8cee0-a03e-430b-9711-420ab0d6a596",
                    Email = "demo@email.com",
                    UserName = "John Doe",
                    PasswordHash = "APc6/pVPfTnpG89SRacXjlT+sRz+JQnZROws0WmCA20+axszJnmxbRulHtDXhiYEuQ==",
                    SecurityStamp = "18272ba5-bf6a-48a7-8116-3ac34dbb7f38"
                };
    
                UserStore.Context.Set<IdentityUser>().AddOrUpdate(demo);
                UserStore.Context.SaveChanges();
            }

    I am getting the error on the line which marked in bold.

    When I have quick watch on UserStore.Context, it is showing me connectionstring, database name etc which is set in web.config file.

    So what I doing wrong here ? Any changes needs to be done here

    Thanks for the help !

    Monday, March 7, 2016 7:16 AM

Answers

  • User-821857111 posted

    If you are connecting with the sa account, remove Integrated Security=true.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, March 7, 2016 8:46 AM

All replies

  • User-744022866 posted

    As mentioned in the error message, you do not have the create permission for the specified database. If you are not intended to use "master" db, make sure you have an initial catalog defined in the connection string that points to your database.

    Monday, March 7, 2016 7:49 AM
  • User-821857111 posted

    The user account that you are connecting to SQL Server with doesn't have permission to create databases. What is your connection string?

    Monday, March 7, 2016 8:04 AM
  • User52625461 posted

    The user account that you are connecting to SQL Server with doesn't have permission to create databases. What is your connection string?

    Here is my connection string. I am using SQL Express

     <connectionStrings>
        <add name="DefaultConnection" connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=SmartAdminMvc;Integrated Security=True;User Id=sa;Password=sql"
          providerName="System.Data.SqlClient" />
      </connectionStrings>

    Monday, March 7, 2016 8:41 AM
  • User52625461 posted

    sreejukg

    As mentioned in the error message, you do not have the create permission for the specified database. If you are not intended to use "master" db, make sure you have an initial catalog defined in the connection string that points to your database.

    Yes, I have defined the initial catalog in the connection string. I am using SQL Express.

    Here is my connection string

     <connectionStrings>
        <add name="DefaultConnection" connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=SmartAdminMvc;Integrated Security=True;User Id=sa;Password=sql"
          providerName="System.Data.SqlClient" />
      </connectionStrings>

    Monday, March 7, 2016 8:42 AM
  • User-821857111 posted

    If you are connecting with the sa account, remove Integrated Security=true.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, March 7, 2016 8:46 AM
  • User52625461 posted

    If you are connecting with the sa account, remove Integrated Security=true.

    Yeah Thanks Mike,

    But now I am getting the below error

    The entity type IdentityUser is not part of the model for the current context.

    Monday, March 7, 2016 8:52 AM
  • User-821857111 posted

    Yeah Thanks Mike,

    But now I am getting the below error

    The entity type IdentityUser is not part of the model for the current context.

    Good. That means your issue is resolved. You should start a new thread for the new issue.

    Monday, March 7, 2016 8:55 AM