Manoz Manoz - 1 month ago 18
C# Question

unable to create DB in SQL server

I am trying to create a Database with entity framework code-first environment.
The tables in database are identity tables.

The problem is with SQL server's database creation. There is no database created When I ran application.

However, I am able to find physical find here at this location -

C:\Users\user\AppData\Local\Microsoft\Microsoft SQL Server Data\SQLEXPRESS\HWAPI.mdf


So whenever I do add any table or change the structure then all the changes are happening in above location and think that this is not using master database.

There is no database created in my local SQL server.

Have a look at my code -

StartUp.cs

public class Startup
{
public void Configuration(IAppBuilder app)
{
HttpConfiguration config = new HttpConfiguration();
ConfigureOAuth(app);
config.DependencyResolver = new NinjectResolver(NinjectWebCommon.CreateKernel());
WebApiConfig.Register(config);
app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll);
app.UseWebApi(config);
}


public void ConfigureOAuth(IAppBuilder app)
{
app.CreatePerOwinContext(OwinAuthDbContext.Create);
app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);
app.CreatePerOwinContext<ApplicationSignInManager>(ApplicationSignInManager.Create);
OAuthAuthorizationServerOptions OAuthServerOptions = new OAuthAuthorizationServerOptions()
{
AllowInsecureHttp = true,
TokenEndpointPath = new PathString("/token"),
AccessTokenExpireTimeSpan = TimeSpan.FromDays(1),
Provider = new AuthorizationServerProvider()
};
}
}


And there is the context -

public class OwinAuthDbContext : IdentityDbContext<ApplicationUser>, IDisposable
{
public OwinAuthDbContext()
: base("DefaultConnection", throwIfV1Schema: false)
{
Database.SetInitializer<OwinAuthDbContext>(null);
}

public static OwinAuthDbContext Create()
{
return new OwinAuthDbContext();
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
modelBuilder.Entity<IdentityUser>().ToTable("AspNetUsers");
modelBuilder.Entity<IdentityRole>().ToTable("AspNetRoles");
modelBuilder.Entity<IdentityUserClaim>().ToTable("AspNetUserClaims");
modelBuilder.Entity<IdentityUserLogin>().ToTable("AspNetUserLogins");
}

protected override void Dispose(bool disposing)
{
base.Dispose(disposing);
}
}


web.config

<configuration>
<configSections>
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</configSections>
<connectionStrings>
<add name="DefaultConnection" connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=HWAPI;Integrated Security=SSPI;Connect Timeout=30;User Instance=True"
providerName="System.Data.SqlClient" />
</connectionStrings>

</configuration>


Please assist and let me know if additional info is required.

Answer

Thanks everybody for putting their inputs.

It turns out that I needed to setup credentials for existing database. After I setup credentials security for database then it worked.

before -

<add name="DefaultConnection" connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=HWAPI;Integrated Security=SSPI;Connect Timeout=30;User Instance=True"
      providerName="System.Data.SqlClient" />

after -

<add name="DefaultConnection" connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=HWAPI;User ID=sa;Password=truw@123" providerName="System.Data.SqlClient"/>

This solved my issue and I was able to connect to database within SQLEXPRESS.

Comments