1

I have built a SQLExpress DB with SSMS and I have used the following command to create the class Models of my DB.

Scaffold-DbContext "Data Source=[Removed]\SQLExpress;Initial Catalog=Warehouse;Integrated Security=True"
Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models –Verbose –Force

Everything seems to create correctly except when I change the name of the DBContext Property to retrieve the entity... Here is what I have:

DB: I have one table called "BOX". This is the class that is created for it from the Scaffold-DBContext call. ID is defined in the DB as the Primary Key.

public class Box
{
    public int Id { get; set; }
    public double Height { get; set; }
    public double Width { get; set; }
    public double Length { get; set; }
}

This is the DBContext class that was created for it:

public class WarehouseContext : DbContext
{
    public WarehouseContext(DbContextOptions<WarehouseContext> options)
        : base(options)
    {
    }

    public DbSet<Box> Box { get; set; }
}

here is the Controller call I make it it:

    public async Task<IActionResult> Index()
    {
        return View(await _BoxDBContext.Box.ToListAsync());
    }

All of this above works perfectly fine, but I don't like that the property in my DBcontext to retrieve boxes is called Box. I want it called Boxes like such.

public class WarehouseContext : DbContext
{
    public WarehouseContext(DbContextOptions<WarehouseContext> options)
        : base(options)
    {
    }

    public DbSet<Box> Boxes { get; set; }
}


    public async Task<IActionResult> Index()
    {
        return View(await _BoxDBContext.Boxes.ToListAsync());
    }

But when I change the name it fails to work, stating the following error:

An unhandled exception occurred while processing the request.

SqlException: Invalid object name 'Boxes'.

System.Data.SqlClient.SqlCommand+<>c. b__107_0(Task result)

I cleaned the Solution multiple time, rebooted the machine and still the error happens...

So I tried this, I added a new property to return the exact same thing, but gave it a different name... So I now have Boxes and TextBoxes

public class WarehouseContext : DbContext
{
    public WarehouseContext(DbContextOptions<WarehouseContext> options)
        : base(options)
    {
    }

    // I left this method here..
    public DbSet<Box> Boxes { get; set; }

    // and added this one.
    public DbSet<Box> TextBoxes { get; set; }

}

    public async Task<IActionResult> Index()
    {
        return View(await _BoxDBContext.Boxes.ToListAsync());
    }

This now works???????

Does anyone know why this might be happening? I obviously can't keep it like this..

Diana
  • 2,186
  • 1
  • 20
  • 31
Michael
  • 145
  • 3
  • 7
  • I'm not entirely sure why the last part worked but seeing that you manually changed the codes, this should be in-sync with your database. If I'm not mistaken, the `Boxes` in `DbSet` should match the table name in DB to work. You might want to try adding migration and updating database. 1. `EntityFrameworkCore\Add-Migration ChangeBoxToBoxes` 2. `EntityFrameworkCore\Update-Database` (This will change the DB table name though) – Lawrence Jul 10 '17 at 13:55
  • Thanks Lawrence, The reason I built the DB first was because I have an exiting DB that I can't change so this option wouldn't work for me. – Michael Jul 10 '17 at 15:07
  • I see. You might want to set the mapping then so that the DbSet is mapped to the correct table. See https://learn.microsoft.com/en-us/ef/core/modeling/relational/tables. In your case, you might need to add this on your OnModelCreating `modelBuilder.Entity() .ToTable("Box");` Hope this will work as I haven't experienced this scenario yet. – Lawrence Jul 10 '17 at 15:15
  • @Lawrence, interesting that worked.. I have an "In Memory Database" example that I built first, but used my Classes to build the DB. In that example I don't have to use the Mapping you defined above... Do you know why I would need it in one and not the other? – Michael Jul 10 '17 at 18:14
  • Since in your testing you built your DB from your classes, entityframework had set up the right mapping for `DbSet` and your DB table. On your original problem though, entityframework DID have the right mapping but you decided to change `DbSet` name. So in this case, you need to manually inform EF that you've made changes and that can be done by overriding `OnModelCreating` which solved your original problem. – Lawrence Jul 11 '17 at 01:03
  • @Lawrence is the Mapping defined somewhere other than the OnModelCreating method? Like in a config or application file somewhere? – Michael Jul 11 '17 at 12:08
  • As far as I know, there are no other configs. All you've got is the snapshot of your EF structure which I think can be found on the migrations folder. This snapshot is getting updated each time you add new migration. As the name suggest, this is just a snapshot so if you want to change something, either add a new migration or override onmodelcreating. – Lawrence Jul 11 '17 at 15:51

0 Answers0