简体   繁体   中英

Entity Framework Code First DB error

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'."}

D B

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

        }
    }

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.

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)

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