1

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.

1 Answer 1

2

When use

Database.SetInitializer<NewsWebsiteContext>(null);

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.