gsiradze
gsiradze

Reputation: 4733

EF not creating database

I've created this project

enter image description here

In data I've installed EF. WebApi is my startup project and it has refference to Business which has reference to Data.

In data I have this:

namespace NewsWebsite.Data
{
    public class NewsWebsiteContext : DbContext
    {
        public NewsWebsiteContext():base("name=NewsWebsiteContext")
        {
            Database.SetInitializer<NewsWebsiteContext>(null);
        }
        public DbSet<Language> Languages { get; set; }
        public DbSet<Post> Posts { get; set; }
        public DbSet<PostLocale> PostLocales { get; set; }
        public DbSet<Role> Roles { get; set; }
        public DbSet<User> Users { get; set; }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
        }
    }
}

And in data's app.config I have:

 <connectionStrings>
    <add name="NewsWebsiteDbContext" connectionString="Server=.\compname;initial catalog=NewsDb;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework" providerName="Syste.Data.SqlClient"/>
  </connectionStrings>

And I have exact same in webapi's web.config (server name is correct).

When I run application it is not creating database. Why?


If someone will face that problem I had 2 mistake:

1) I had base("name=NewsWebsiteContext") instead of NewsWebsiteDbContext

2) I had providerName="Syste.Data.SqlClient" instead of System.Data.SqlClient

and I was not using migrations. Just do

enable-migrations -EnableAutomaticMigration:$true and then update-database that's all.

Upvotes: 1

Views: 674

Answers (1)

Soheil Alizadeh
Soheil Alizadeh

Reputation: 3066

When use

Database.SetInitializer<NewsWebsiteContext>(null);

EF not create automatic database. Try add migration and update database

Upvotes: 2

Related Questions