简体   繁体   English

实体框架代码第一个DB错误

[英]Entity Framework Code First DB error

I'm trying to make a web application MVC4 in c#. 我正在尝试在c#中创建一个Web应用程序MVC4。

I'm using the same DB than the DB created initially for users (anuthentication) (more easy to deal in connexionstrings). 我使用的数据库与最初为用户创建的数据库(anuthentication)相同(更容易处理连接字符串)。

So I made 3 models and the models were find in DB. 所以我制作了3个模型,模型在DB中找到。 Now I added another entity as a model, but the model is'nt create in the mdf file. 现在我添加了另一个实体作为模型,但模型不是在mdf文件中创建的。

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 目前,一切正常,除了处理我最新实体的控制器(名为“ItemsToBuy”),因为它确实不存在于DB中

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) : 使用该异常(InnerException):

{"Invalid object name 'dbo.ItemsToBuys'."} {“无效的对象名称'dbo.ItemsToBuys'。”}

D B

And the DBCOntext class : 和DBCOntext类:

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 : 并根据需要在global.asax中:

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: 您可以使用另一个EF内置初始化程序:

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. 如果模型中没有变化,则DB保持不变。

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. 否则,将返回错误,告知EF不能删除数据库。

you need to enable EF migrations. 您需要启用EF迁移。

Using Nuget Package Manager Console run, Enable-Migrations. 使用Nuget Package Manager控制台运行,启用 - 迁移。

The full tutorial is Building an Initial Model & Database 完整的教程是构建初始模型和数据库

The commands from the Nuget console will look similar to Nuget控制台的命令看起来类似于

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

and update the database afterwards (Update-Database) 然后更新数据库(Update-Database)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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