1

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

}

}
1
  • And with Database.SetInitializer<StoreDBContext>(new DropCreateDatabaseIfModelChanges<StoreDBContext>()) or DropCreateDatabaseAlways? (will delete your data base) Commented Jul 12, 2017 at 15:10

1 Answer 1

2

Specify a DB initializer in your DB context

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

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

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

5 Comments

Hi, thank you for your help! I'm going to trial this now and I'll let you know if this corrects my error:)
@Evar If I understand your correct, you don't have any data yet. In this case, did you try ´new DropCreateDatabaseIfModelChanges<StoreDBContext>()´ ?
I haven't tried that, my user database is seeded with an username and password, when logged in I would be able to add data in; no tables have been created on my sql server which is the issue i'm trying to overcome.
@Evar then you can try it. It recreates all tables in NewWebsite2017But doesn't mess with any permissions set
Unfortunately still no no luck with this suggestion either

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.