首页 > 解决方案 > 为什么在我使用控制器添加迁移后没有生成我的表?

问题描述

我正在 Visual Studio 社区中构建一个应用程序,并试图让我创建的模型显示在我的数据库中的表中。出了点问题,但我没有足够的经验来发现问题。我花了几个小时试图搜索尽可能多的关键术语,但我没有找到很多有用的信息。

确切的问题:到目前为止,我正在创建两个模型,称为 Alert 和 MenuItem。Alert 模型有一个为它制作的脚手架控制器,并在数据库中实现,它可以按预期完美运行。但是在回到数据库方面之后,我没有得到相同的结果。我创建了 MenuItem 模型,为其创建了一个新的脚手架项目,运行了 Add-Migration 命令,然后运行了 Update-Database 命令。当我刷新 SQL Server 对象资源管理器时,只有警报表。以下是我的一些文件,可能有助于查看,但我真的不明白为什么数据库不添加表,就像我被教导它会添加它一样。

菜单项.cs

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;

namespace _6thStreetDive.Models
{
    public class MenuItem
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string Desc { get; set; }
        public bool ShowMainPrice { get; set; }

        [Column(TypeName = "decimal(18,4)")]
        public decimal MainPrice { get; set; }
        public string Category { get; set; }

        [NotMapped]
        public Dictionary<string, decimal> Options { get; set; }
    }
}

菜单项控制器.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using _6thStreetDive.Data;
using _6thStreetDive.Models;

namespace _6thStreetDive.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class MenuItemsController : ControllerBase
    {
        private readonly _6thStreetDiveContext _context;

        public MenuItemsController(_6thStreetDiveContext context)
        {
            _context = context;
        }

        // GET: api/MenuItems
        [HttpGet]
        public async Task<ActionResult<IEnumerable<MenuItem>>> GetMenuItem()
        {
            return await _context.MenuItem.ToListAsync();
        }

        // GET: api/MenuItems/5
        [HttpGet("{id}")]
        public async Task<ActionResult<MenuItem>> GetMenuItem(int id)
        {
            var menuItem = await _context.MenuItem.FindAsync(id);

            if (menuItem == null)
            {
                return NotFound();
            }

            return menuItem;
        }

        // PUT: api/MenuItems/5
        // To protect from overposting attacks, see https://go.microsoft.com/fwlink/?linkid=2123754
        [HttpPut("{id}")]
        public async Task<IActionResult> PutMenuItem(int id, MenuItem menuItem)
        {
            if (id != menuItem.Id)
            {
                return BadRequest();
            }

            _context.Entry(menuItem).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!MenuItemExists(id))
                {
                    return NotFound();
                }
                else
                {
                    throw;
                }
            }

            return NoContent();
        }

        // POST: api/MenuItems
        // To protect from overposting attacks, see https://go.microsoft.com/fwlink/?linkid=2123754
        [HttpPost]
        public async Task<ActionResult<MenuItem>> PostMenuItem(MenuItem menuItem)
        {
            _context.MenuItem.Add(menuItem);
            await _context.SaveChangesAsync();

            return CreatedAtAction("GetMenuItem", new { id = menuItem.Id }, menuItem);
        }

        // DELETE: api/MenuItems/5
        [HttpDelete("{id}")]
        public async Task<IActionResult> DeleteMenuItem(int id)
        {
            var menuItem = await _context.MenuItem.FindAsync(id);
            if (menuItem == null)
            {
                return NotFound();
            }

            _context.MenuItem.Remove(menuItem);
            await _context.SaveChangesAsync();

            return NoContent();
        }

        private bool MenuItemExists(int id)
        {
            return _context.MenuItem.Any(e => e.Id == id);
        }
    }
}

上下文.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using _6thStreetDive.Models;

namespace _6thStreetDive.Data
{
    public class _6thStreetDiveContext : DbContext
    {
        public _6thStreetDiveContext (DbContextOptions<_6thStreetDiveContext> options)
            : base(options)
        {
        }
        public DbSet<_6thStreetDive.Models.Alert> Alert { get; set; }
        public DbSet<_6thStreetDive.Models.MenuItem> MenuItem { get; set; }

    }
}

我尝试过的事情:

所以在这一点上,我不知道该怎么做。我可能在某个我不知道要寻找的地方错过了一个步骤,因为我的大学课程都没有教我如何一次在这个 IDE 中处理多个数据库。但我按步骤顺序做的是:

标签: c#sqlvisual-studioentity-framework-migrations

解决方案


推荐阅读