简体   繁体   English

通过使用EntityFramework添加新记录

[英]Adding a new record by using EntityFramework

I am creating a test project in order to learn using asp.net 5 and the mvc 6 framework. 我正在创建一个测试项目,以学习使用asp.net 5mvc 6框架。

I have decided to create a simple webpage that each menu item comes from the database. 我决定创建一个简单的网页,每个菜单项都来自数据库。 To do so I have created a model like such 为此,我创建了一个像这样的模型

namespace TestTemplate.Models
{
    public class SideMenuItem
    {
        public int Id { get; set; }

        public string Level { get; set; }

        public string Label { get; set; }

        public string Link { get; set; }
    }
}

Inside my Models folder I also have a file named `TestContext.cs' 在“模型”文件夹中,我还有一个名为“ TestContext.cs”的文件

namespace TestTemplate.Models
{
    public class TestContext : DbContext
    {
        public DbSet<SideMenuItem> SideMenuItems { get; set; }
    }
}

That is my EntityFramework DbContext class. 那就是我的EntityFramework DbContext类。

When trying to create a new SideMenu item by using a simple view with a form to adding all the needed data, then using my angular factory that looks like this 当尝试通过使用带有窗体的简单视图添加所有需要的数据来创建新的SideMenu项时,然后使用如下所示的我的角度工厂

return $resource('/api/sidemenu/:id');

I get the error: 我收到错误:

An exception of type 'Microsoft.Data.Entity.DbUpdateException' occurred in EntityFramework.Core.dll but was not handled in user code

Additional information: An error occurred while updating the entries. See the inner exception for details.

-->System.Data.SqlClient.SqlException: Invalid object name 'SideMenuItem'.

This error occurs on SideMenuController.cs where I define my API at the part where I am trying to Post the new item: 此错误发生在SideMenuController.cs ,我在其中尝试发布新项目的部分定义了我的API:

[HttpPost]
public IActionResult Post([FromBody]SideMenuItem sideMenuItem)
{
    if (sideMenuItem.Id == 0)
    {
        _dbContext.SideMenuItems.Add(sideMenuItem);
        _dbContext.SaveChanges();                        // ERROR HERE.
        return new ObjectResult(sideMenuItem);
    }
    else
    {
        var original = _dbContext.SideMenuItems.FirstOrDefault(m => m.Id == sideMenuItem.Id);
        original.Level = sideMenuItem.Level;
        original.Label = sideMenuItem.Label;
        original.Link = sideMenuItem.Link;
        _dbContext.SaveChanges();
        return new ObjectResult(original);
    }
}

I also should mention that before running the app i used 我还应该提到,在运行我使用的应用程序之前

>dnx ef migration add initial
>dnx ef database update

I believe it has to do with me not creating my database correctly. 我认为这与我无法正确创建数据库有关。 Since I am not seeing any folder on my project that had anything to do with databases or migrations. 由于我没有在项目中看到任何与数据库或迁移有关的文件夹。

Why is it complaining that SideMenuItem is invalid, and how can I fix the issue? 为什么抱怨SideMenuItem无效,我该如何解决此问题?

After trying to create my database again, I noticed that I had a typo on my migration command, hence the database was not created. 再次尝试创建数据库后,我注意到我的迁移命令输入错误,因此未创建数据库。

The command should have been dnx ef migrations ... with an s. 该命令应该是dnx ef migrations ...带有s。

A good starting point with all the commands can be found here . 这里可以找到所有命令的良好起点。

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

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