代码frist方法给出以下错误 [英] Code frist approach is giving following error

查看:51
本文介绍了代码frist方法给出以下错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我正在尝试编写使用Entity Framework和Web Api的Code First应用程序。当我理想地运行应用程序时,Code First应该在指定的服务器上为我创建一个数据库。但是当我尝试给它时出现以下错误。

Hi All,

I am trying to write a Code First Application which is using Entity Framework and Web Api. When I run the application ideally the Code First should create a database for me on the specified Server. But when I am trying its giving me the following error.

The model backing the 'MVCDBContext' context has changed since the database was created. Consider using Code First Migrations to update the database (http://go.microsoft.com/fwlink/?LinkId=238269).



这是我的控制器


Here is my Controller

namespace MVCWithWebApiApp.Controllers
{
    public class UserController : Controller
    {
        public ActionResult Index()
        {
            var dbContext = new MVCDBContext();

            List<User> listOfEmployees;
            //var a = oDAL.employees;
            listOfEmployees = dbContext.Users.ToList();
            return Json(listOfEmployees, JsonRequestBehavior.AllowGet);

            //return View();
        }

        //
        // GET: /User/Details/5

        public ActionResult Details(int id)
        {
            return View();
        }

        //
        // GET: /User/Create

        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /User/Create

        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                // TODO: Add insert logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /User/Edit/5

        public ActionResult Edit(int id)
        {
            return View();
        }

        //
        // POST: /User/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /User/Delete/5

        public ActionResult Delete(int id)
        {
            return View();
        }

        //
        // POST: /User/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}





这是我的Db Context < br $>



And here is my Db Context

namespace MVCWithWebApiApp.Models
{
    public class MVCDBContext : DbContext
    {
        public DbSet<User> Users { get; set; }
    }
}





这是我的模特



And here is my Model

namespace MVCWithWebApiApp.Models
{
    public class User
    {
        private int _userId;
        public int UserId
        {
            get { return _userId; }
            set { _userId = value; }
        }

        private string _userName;
        public string UserName
        {
            get { return _userName; }
            set { _userName = value; }
        }
        
        
    }
}



当我试图称之为 [ ^ ]



然后它在Action方法中的突破点public ActionResult Index()



但是我给出了上面提到的错误

我在Web Config中的连接字符串是:


When I am trying to call the as "" "[^]

Then it is hitting the break point in the Action method "public ActionResult Index()"

But giving me the error as I mentioned above
My Connection string in Web Config is:

<add name="MVCDBContext" providerName="System.Data.SqlClient" connectionString="Data Source=KNW-DEL-ABDUL\SQL2008R2;Integrated Security=True;Connect Timeout=15;Encrypt=False;TrustServerCertificate=False"/>





请帮我看看如何使用Code First方法在后端生成数据库。

请告诉我如何在不丢失现有数据的情况下更新数据库上的更改。这意味着我在创建数据库后如何在Code First中思考可以在数据库上实现新的架构更改等而不会影响现有数据吗?



提前致谢。



我尝试了什么:



尝试在线研究并从朋友那里获取帮助等,但如果可以请帮助我



Please help me how to generate a database on the back-end using Code First approach.
And please let me know how can I update changes on the database without losing already existing data. Means I am thinking in Code First once the database is created then how can implement new schema changes etc on database without effecting the existing data?

Thanks in advance.

What I have tried:

Trying to research online and taking help from friends etc, but if you can please help me

推荐答案

您正在使用Entity Framework编写ASP.NET MVC应用程序及其Code First方法,而不是Code First应用程序。



错误消息告诉您,自从服务器中的数据库创建以来,您修改了DbContext类和/或它使用的模型类。



如果您仍在创建数据库并且不关心其中的数据,您可以从SQL Server中删除数据库并重新运行您的应用程序,它将重新创建数据库。



或者,如果您关心数据库中的数据,您必须学习如何进行数据库迁移。 实体框架代码优先迁移 [ ^ ]
You're writing a ASP.NET MVC application using Entity Framework with its Code First approach, not a "Code First application".

The error message is telling you that you modified either your DbContext class and/or the your model classes that it uses since the database in the server was created.

If you're still creating the database and you don't care about the data in it you can just delete the database from SQL Server and re-run your app and it'll recreate the database.

Or if you care about the data that in the database, you have to learn how to do database migrations. Entity Framework Code First Migrations[^]


这篇关于代码frist方法给出以下错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆