简体   繁体   中英

Why are my database tables not generating, my connection string is correct? asp.net mvc

I'm relatively new to using mvc , I have created an application with several models with scaffold controllers and CRUD views . When running my application on visual studio's local host and localdb my application works perfectly, however when trying to deploy my application I am receiving an error,

error processing your request

on the pages associated with my models.

I have deployed my project on a production iis server, and have setup a user login and database on an ms sql server. My project has the correct connection string to interact with the sql server, however when observing the database, my tables associated with my projects models are not being created - which is the route cause of the errors I am receiving.

Is anyone able to suggest why my tables are not being created; below are examples of my code, please note I am also using the asp.net Identity functionality, these tables are also not being created:

Example of one of my models:

using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;

namespace newwebsite.Models
{
    public class Store
    {
    public int ID { get; set; }
    public string ItemName { get; set; }
    public string ItemImageName { get; set; }
    public decimal ItemPrice { get; set; }
    public string ItemDescription { get; set; }
    public string ItemFeatures { get; set; }
}
public class StoreDBContext : DbContext
{
    public DbSet<Store> Blog { get; set; }

    public StoreDBContext()
        : base("DefaultConnection")
    {
    }

    public static StoreDBContext Create()
    {
        return new StoreDBContext();
    }
}
}

Example of my connection string:

<add name="DefaultConnection" connectionString="Data Source='MSSqlServerIP'; Initial Catalog=NewWebsite2017; User ID=NewWebsite2017; Password=*****" providerName="System.Data.SqlClient" />

When I've deployed my project all views are working which suggests my controllers for these views are fine. I'm purely getting the error wherever data from the database is supposed to be shown. I believe these errors are due to my tables not being created, but am unsure why this is.

Update I have edited the model (and others in accordingly) as follows, however still have no luck.

using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;

namespace newwebsite.Models
{
public class Store
{
    public int ID { get; set; }
    public string ItemName { get; set; }
    public string ItemImageName { get; set; }
    public decimal ItemPrice { get; set; }
    public string ItemDescription { get; set; }
    public string ItemFeatures { get; set; }
}
public class StoreDBContext : DbContext
{
    public DbSet<Store> Blog { get; set; }

    public StoreDBContext()
: base("DefaultConnection")
{
        Database.SetInitializer<StoreDBContext>(new    CreateDatabaseIfNotExists<StoreDBContext>());
    }

}

}

Specify a DB initializer in your DB context

public StoreDBContext()
    : base("DefaultConnection")
{
   Database.SetInitializer<StoreDBContext>(new CreateDatabaseIfNotExists<StoreDBContext>());
}

There are several other initializers. eg DropCreateDatabaseIfModelChanges or DropCreateDatabaseAlways (be careful. this can delete your data ) or you can program your own initializer.

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