Codefirst数据库没有创建

本文关键字:创建 数据库 Codefirst | 更新日期: 2023-09-27 18:13:53

我正在尝试创建一个实体框架代码优先环境的数据库。数据库中的表是标识表。

问题是SQL server的数据库创建。当我运行应用程序时没有创建数据库。

然而,我能够在这个位置找到物理发现-

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

所以每当我添加任何表或改变结构时,所有的变化都发生在上面的位置,并认为这不是使用主数据库。

我的本地SQL服务器没有创建数据库

看看我的代码-

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()
            };
       }
    }

还有上下文 -

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>

如果需要额外的信息,请帮助我。

Codefirst数据库没有创建

您的问题是连接字符串。您需要尝试如下所示。

如果需要使用Localhost,则:

<connectionStrings>
 <add name="DefaultConnection" connectionString="Server=localhost; Database=HWAPI;Trusted_Connection=True;" providerName="System.Data.SqlClient" />
</connectionStrings>

如果需要使用LocalDb,则:

<connectionStrings>
  <add name="DefaultConnection" connectionString="Data Source=(LocalDB)'v11.0;AttachDbFilename=|DataDirectory|'HWAPI.mdf;Integrated Security=True" providerName="System.Data.SqlClient" />
</connectionStrings>

感谢大家的投入。

原来我需要为现有数据库设置凭据。在我为数据库设置凭据安全之后,它工作了。

before -

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

-

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

这解决了我的问题,我能够连接到数据库内的SQLEXPRESS