Compuskills-Jerusalem / mens-capstone-2020

1 stars 0 forks source link

Database creation problem #59

Closed nossondsimon closed 4 years ago

nossondsimon commented 4 years ago

While trying to create a database for the project it was noted that a new database was not being created, rather, the program was using the DefaultConnection that was automatically created. This was problomatic because I was specifically attempting to reference a new database in the connection string -- connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Mens2020Capstone.mdf;Integrated Security=True"
providerName="System.Data.SqlClient" />

To look into the problem further, I checked out where the default database is actually created in IdentityModels.cs the code:

public class ApplicationDbContext : IdentityDbContext { public ApplicationDbContext() : base("DefaultConnection", throwIfV1Schema: false) }

public static ApplicationDbContext Create() { return new ApplicationDbContext(); }

To look further into why this was not working in tandem with the connection string I set up, but instead with the default database I looked at the source code for IdentityDbContext.cs in EF here : https://github.com/aspnet/AspNetIdentity/blob/master/src/Microsoft.AspNet.Identity.EntityFramework/IdentityDbContext.cs the relevant code is lines 24-40 :

public class IdentityDbContext : IdentityDbContext<IdentityUser, IdentityRole, string, IdentityUserLogin, IdentityUserRole, IdentityUserClaim> { ///

/// Default constructor which uses the DefaultConnection /// public IdentityDbContext() : this("DefaultConnection") { }

    /// <summary>
    ///     Constructor which takes the connection string to use
    /// </summary>
    /// <param name="nameOrConnectionString"></param>
    public IdentityDbContext(string nameOrConnectionString)
        : base(nameOrConnectionString)
    {
    }

You can see that without the parameter of "nameOrConnectionString" , the DefaultConnection will be used as the connection. To fix this I added code to change the route that ApplicationDbContext would use. Now it is passed a string that will be taken from the Web.config file where the connection string is. Then I added a new connection string to web.config in order to work with the identity framework. <add name="ApplicationDbContext" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Mens2020Capstone.mdf;Integrated Security=True" providerName="System.Data.SqlClient" />

After this, I was thrown an error stating that there was a permissions error with creating the database.

image

After some research I found that I needed to alter the permissions of SQL Server, source: https://stackoverflow.com/questions/18286765/sql-server-operating-system-error-5-5access-is-denied/35464963

step by step: Control Panel -> System and Security -> Administrative Tools -> Services -> Double Click SQL Server (SQLEXPRESS) -> right-click, Properties Select Log On Tab Select "Local System Account" (the default was some obtuse Windows System account) -> OK right click, Stop right click, Start

Now the database CRUD should work properly. For some reason, the database name on my computer is the file path and not the actual mdf file name. Need to figure out why.