I'm building an app in Visual Studio Community and I'm trying to get the models that I've created to show up in a table in my database. Something is going wrong but I'm not experienced enough to find the problem. I've spent a couple hours trying to google as many key terms as I can but I'm not finding a lot of helpful information.
The exact problem: I'm creating two models so far called Alert and MenuItem. The Alert model had a scaffolded controller made for it and was implemented into the database and it works perfectly as intended. But after coming back to the database side of things, I'm not getting the same results. I created my MenuItem model, Created a new scaffolded item to it, ran the Add-Migration command, and ran the Update-Database command. And when I refresh the SQL Server Object Explorer there is only the Alert table. Here are some of my files that might be helpful to look at but I'm really not understanding why the database doesn't add the table like I've been taught it would add it.
MenuItem.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; }
}
}
MenuItemsController.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);
}
}
}
Context.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; }
}
}
Things I've tried:
- Deleting migrations and starting over with the InitialCreate.
- Added new Migrations
- Deleting the controller for MenuItem and running the scaffolding again
- Close Visual Studio and restart it
- Rebuild the project and repeat previous steps
So at this point I'm not sure what to do. I might be missing a step somewhere that I just don't know to look for because none of my college classes taught me how to do multiple databases in this IDE at a time. But what I did in order of steps was:
- Create the Model
- Add New Scaffolded Item
- Add-Migration "Naming thing here"
- Update-Database
- Refresh SSOE viewer