c# - 为什么在我使用控制器添加迁移后没有生成我的表?
问题描述
我正在 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; }
}
}
我尝试过的事情:
- 删除迁移并从 InitialCreate 重新开始。
- 添加了新的迁移
- 删除 MenuItem 的控制器并再次运行脚手架
- 关闭 Visual Studio 并重新启动它
- 重建项目并重复前面的步骤
所以在这一点上,我不知道该怎么做。我可能在某个我不知道要寻找的地方错过了一个步骤,因为我的大学课程都没有教我如何一次在这个 IDE 中处理多个数据库。但我按步骤顺序做的是:
- 创建模型
- 添加新的脚手架项目
- 添加迁移“在此处命名”
- 更新数据库
- 刷新 SSOE 查看器
解决方案
推荐阅读
- flutter - 如果文本很长,如何使文本移至新行
- python - 无法操作 Tensorflow:setuptools 已过时
- bootstrap-4 - 带有ul和列的bootstrap 4 css在Safari上不起作用
- python - 我已经下载了 pyinstaller,但是当我尝试转换 python 文件时,它说 [Errno 13] Permission denied
- python - 如何循环遍历 88 个元素的列表的排列?
- python - Pandas 数据框-TypeError:'_io.TextIOWrapper' 类型的对象没有 len()
- ubuntu - 我的 Nginx 配置是 500 内部服务器错误的罪魁祸首吗?
- makefile - 为什么我的 Makefile 规则没有删除 .o 文件?
- javascript - Node.js 无法导入 ES6 模块
- php - 从输入字段中减去小时和分钟