0

I'm trying to make a web application MVC4 in c#.

I'm using the same DB than the DB created initially for users (anuthentication) (more easy to deal in connexionstrings).

So I made 3 models and the models were find in DB. Now I added another entity as a model, but the model is'nt create in the mdf file.

How Can I create it from code or rebuild the DB, or...

For the moment, all works fine except with the controllers that are dealing of my latest entity (named "ItemsToBuy") because it doens't exist in DB indeed

Thanks to help me!

EDIT : CODE

namespace MvcShop.Models
{
    public class ItemsToBuy
    {

        public int ItemsToBuyId {get; set;}

        public Item Item { get; set; }

        public int NumberItems { get; set; }
        public string AddedBy { get; set; }
        public DateTime AddedDate { get; set; }

        public int ItemId { get; set; }

    }
}

And the method that make the exception :

var itemstobuys = db.ItemsToBuy.Include(i => i.Item);
return View(itemstobuy.ToList());

With that Exception (InnerException) :

{"Invalid object name 'dbo.ItemsToBuys'."}

DB

And the DBCOntext class :

namespace MvcShop.Models
{
    public class ShopEntities : DbContext
    {
        public DbSet<Item> Item { get; set; }
        public DbSet<ItemShop> ItemShop { get; set; }
        public DbSet<ItemsToBuy> ItemsToBuy { get; set; }
        public DbSet<Shop> Shop { get; set; }
    }
}

and in global.asax as required :

public class MvcApplication : System.Web.HttpApplication
    {
        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();

            WebApiConfig.Register(GlobalConfiguration.Configuration);
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);
            AuthConfig.RegisterAuth();

            Database.SetInitializer<ShopEntities>(null);

        }
    }
6
  • You'll need to add your code Commented Jun 15, 2013 at 10:05
  • @devdigital : Thanks . I made edits, but I don't think that it add comprehension to the problem, it's clear for me, but the solution hmmmmmmm :-/ ;-) Commented Jun 15, 2013 at 11:59
  • have you done right button on the mdf file and 'refresh'? And could you share your DbContext class and your DB Initialization code? Commented Jun 15, 2013 at 12:02
  • @Vic hello thanks. I've edited the initial post again :-) Commented Jun 15, 2013 at 17:04
  • Thanks, still missing your DB initialization code. Most probably placed in global.asax Commented Jun 15, 2013 at 18:11

2 Answers 2

2

The default initializer (the one you are using) just create the DB if it does not exists already in the database. You could use another EF built-in initializer:

Database.SetInitializer(new DropCreateDatabaseIfModelChanges<ShopEntities>());

This way, the initializer will drop the database and create a new one again with the changes of your model when you do your first access to the DB and the model has changed. If there is no changes in the model, DB remains as it is.

Before running the app be sure there is no existing opened connections in the DB. Otherwise you will be returned an error telling that EF cannot drop the database.

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

Comments

0

you need to enable EF migrations.

Using Nuget Package Manager Console run, Enable-Migrations.

The full tutorial is Building an Initial Model & Database

The commands from the Nuget console will look similar to

Enable-Migrations -ContextTypeName Table.Models.Data.DatabaseContext -EnableAutomaticMigrations -Force) 

and update the database afterwards (Update-Database)

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.