0

I am a new C# application in Visual Studio 2022. The default application contains a HomeController with the standard Index and Privacy. I created 3 model entities. They are called Box, Item, and Room. I was able to add a new controller and scaffolded the Box model. EntityFramework created the controller along with the appropriate views. I also created the database context and added this code to the BoxController:

 private readonly DatabaseContext _context;

 public BoxesController(DatabaseContext context)
 {
    _context = context;
 }

This is all good.

The code in the BoxesConroller Index action looks like this:

 public async Task<IActionResult> Index()
 {
      return _context.Box != null ? 
           View(await _context.Box.ToListAsync()) :
           Problem("Entity set 'DatabaseContext.Box'  is null.");
 } 

And when I run the application and navigate to

https://localhost:7046/boxes

The resulting view displays the contents of the Box database table. This is exactly what I had expected.

I then copied the pertinent code from BoxesController into HomeController and I am able to launch the application and display the contents of the Box table when I navigate to

https://localhost:7046

This is also good.

This is the code that I am using in HomeController to retrieve data from the database and pass it along to the view:

    public IActionResult Index()
    {
        var BoxToSend = new List<Box>();
        var Boxes = _context.Box.ToList();
        foreach (var box in Boxes)
        {
            Box TempBox = new Box();
            TempBox.BoxId = box.BoxId;
            TempBox.BoxDescription = box.BoxDescription;
            TempBox.RoomName = "unspecified room";
            TempBox.RoomNumber = box.RoomNumber;
            if (TempBox.RoomNumber > 0)
            {
                TempBox.RoomName = "from database";
                BoxToSend.Add(TempBox);
            }
        }

        if (BoxToSend.Count > 0)
        {
            var results = BoxToSend.OrderBy(x => x.RoomName).ToList();
            BoxToSend = results;
        }

        return View(BoxToSend);
    }

If TempBox.RoomNumber is greater than 0, I need to pull a record from the Room database table where the RoomID = TempBox.RoomNumber. This should return a string with a room name.

The problem that I am having is when I type in _context. in HomeController, visual studio presents me with the popup box of properties associated with _context. The only database table present is Box.

I assume that since the Box model is the only model that I did a scaffolding on (sorry if my terminology is incorrect), that is the only table that _context knows about.

How can I scaffold the remaining Models (Room and Item) so they would become known to _context?

Thank you!

phuzi
  • 12,078
  • 3
  • 26
  • 50
Jonathan Small
  • 1,027
  • 3
  • 18
  • 40
  • Scaffolding generates views and controllers based on entities. Instead of scaffolding, you just need to declare a `DbSet Boxes { get; set; }` property on your DbContext. – CodeCaster Sep 08 '22 at 14:36
  • I found the problem. In my datacontext file, I had an a reference to the table as Rooms when it should have been Room. All's good now. Thank! – Jonathan Small Sep 08 '22 at 15:10

0 Answers0