简体   繁体   中英

Upload ASP WebProject to WebHosting with Code-First EntityFramework - CREATE DATABASE 'master'

So I build my asp.net web project using MVC and it's working fine in localhost using code-first approach of EntityFramework 6.0.

This week I bought a new Web Hosting to upload my website.

The problem is that, everytime I run the website it tries to create a database in database 'master', why is this happening?

I am selecting the right database that my Web Hosting provided on my ConnectionString.

There is a note: I am also using ASP Identity OWIN... maybe that's the problem? it's using another ConnectionString ?

My Identity Config (Used for OWIN):

namespace MyNamespace
{
    public class IdentityConfig
    {
        public void Configuration(IAppBuilder app)
        {
            app.CreatePerOwinContext(() => new MyContext());
            app.UseCookieAuthentication(new CookieAuthenticationOptions
            {
                AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
                LoginPath = new PathString("/Home/Login"),
            });
            FormsAuthentication.SetAuthCookie("CookieValue", false);

        }
    }
}

And here is my DbContext called MyContext:

namespace MyNamespace.DAL
{
    public class MyContext : IdentityDbContext<IdentityUser>
    {
        public MyContext() : base("MyNamespace.DAL.MyContext")
        {
            if (!Roles.Any(r => r.Name == "admin"))
            {
                var RoleManager = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>(this));
                RoleManager.Create(new IdentityRole("admin"));
            }                


            if (!Users.Any(u => u.UserName == "myuser"))
            {
                var store = new UserStore<IdentityUser>(this);
                var manager = new UserManager<IdentityUser>(store);

                var user = new IdentityUser { UserName = "myuser" };

                manager.Create(user, "mypw123");
                manager.AddToRole(user.Id, "admin");
            }
        }

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

        public DbSet<Item1> stuff1 { get; set; }
        public DbSet<Item2> stuff2 { get; set; }
        public DbSet<Item3> stuff3 { get; set; }
        public DbSet<Item4> stuff4 { get; set; }
        public DbSet<Item5> stuff5 { get; set; }
    }
}

Here is my Web.Config:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections></configSections>

  <appSettings>
    <add key="owin:AppStartup" value="MyNamespace.IdentityConfig" />
  </appSettings>

  <connectionStrings>
    <add name="MyNamespace.DAL.MyContext" providerName="System.Data.SqlClient" connectionString="server=myhostingmssql07; uid=myuserid_en_wmx00; database=mydbname_en_wmx00; password=mybdpw;" />
  </connectionStrings>

  <system.web>
    <compilation debug="true" targetFramework="4.5" />
    <httpRuntime targetFramework="4.5" />
    <customErrors mode="Off"/>
    <trust level="Full" />
  </system.web>

  <!--More stuff here..-->

</configuration>

PS: I know that this mechanism I used to create default admins is not the best approach, I will change it to a seed method later, I just need to put this working right now, then I will handle all the refactoring.


PS2: The log message causing the error (I am using customErrors = Off as you can see above) is on the following line:

if (!Roles.Any(r => r.Name == "admin"))

This line triggers a create database on database 'master' , that's not what is specified on ConnectionString!!

It should create all the DbContext and IdentityDbContext tables on the Database called "mydbname_en_wmx00" as you can see above in ConnectionString..

I tried using EntityFramework Default Factory on Web.Config using as parameter value the same ConnectionString and I started getting (Internal Server Error 500), so I just rolled back and deleted this EntityFramework Default Factory configuration...


PS3: I know that if I use the following Initializer on MyContext constructor:

Database.SetInitializer<MyContext>(null);

it doesn't trigger any CREATE on Database and everything goes okay, the problem is that I need to set up everything manually on Database, like AspNetRoles and AspNetUsers, etc etc, and all my DbContext Tables (Stuff1, stuff2, etc).


PS4: Maybe you are thinking this link is a duplicate question, but it's not, he is using local SQLEXPRESS instance, and I am using a remote mssql from my paid host...


Thank you so much for your effort, do you know by any chance, what is happening here? EF Code-First auto-generate databases don't work on Paid-Web-Hostings??

It seems it's a problem from my paid web-hosting..

There isn't support for code-first approaches (that includes running Update Migrations and Seed Methods from Package Manager Console), because it always reference the 'master' Database which I don't have permissions, as you might know..

So I figured it out a workaround, I generate a full SQL Script from my Context and Models and then I just run the SQL Script on my Remote Database Server using a tool from their system, and all the tables are created according to each model specification.

To generate the SQL Script I run the following command on the Package Manager Console (make sure you typed Enable-Migrations before - if you can't Enable-Migrations because your remote host on the ConnectionString doesn't allow it, just create a new ConnectionString pointing to your local db and then run again " Enable-Migrations -Force " it will work, it did for me):

Update-Database -Script -SourceMigration:0

Then don't forget to tell your EF Contexts that you are using a null DatabaseInitializer so it doesn't try to drop/create a new Database using code-first approach:

Database.SetInitializer<YourContext>(null);

Thanks to StackOverFlow I found this solution, how lucky we are nowadays!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM