开发者

EF 4.1 code first still looking for old table that no longer exists

I am using Entity Framework 4.1 code first.

I had a table in the database with the name MaritalStatus. I deleted it and created a new table in its place called MaritalStatuses. Whenever I try to get all the records from the table I get an error:

Invalid object name 'dbo.MaritalStatus'.

The query that it is trying to execute is:

SELECT 
[Extent1].[Id] AS [Id], 
[Extent1].[Name] AS [Name], 
[Extent1].[IsActive] AS [IsActive]
FROM [dbo].[MaritalStatus] AS [Extent1]}

Why would it still be looking for table MaritalStatus that I deleted? Can it be possible that it is cached somewhere? I dropped the whole database and recreated it via scripts. Still no luck. Maybe it has an issue when it comes to the "es" part of the name?

Context class:

public class HefContext : DbContext
{
   public DbSet<Bank> Banks { get; set; }
   public DbSet<AccountType> AccountTypes { get; set; }
   public DbSet<MaritalStatus> MaritalStatuses { get; set; }
}

View model (with partial properties):

public class EditGrantApplicationViewModel
{
   public int Id { get; set; }
   public string EmployeeNumber { get; s开发者_开发技巧et; }
   public string FirstName { get; set; }
   public string LastName { get; set; }
   public int MaritalStatusId { get; set; }
   public IEnumerable<MaritalStatus> MaritalStatuses { get; set; }
   public int BankId { get; set; }
   public IEnumerable<Bank> Banks { get; set; }
   public int AccountTypeId { get; set; }
   public IEnumerable<AccountType> AccountTypes { get; set; }
}

Dropdown list in view for MaritalStatuses:

<td><label>Marital Status:</label> <span class="red">**</span></td>
<td>
   @Html.DropDownListFor(x => x.MaritalStatusId, new SelectList(Model.MaritalStatuses, "Id", "Name", Model.MaritalStatusId), "-- Select --")
   @Html.ValidationMessageFor(x => x.MaritalStatusId)
</td>

Controller:

public ActionResult Create()
{
   EditGrantApplicationViewModel viewModel = new EditGrantApplicationViewModel
   {
      MaritalStatuses = maritalStatusService.GetAll(),
      Banks = bankService.GetAll(),
      AccountTypes = accountTypeService.GetAll()
   };

   return View(viewModel);
}

Service:

public IEnumerable<MaritalStatus> GetAll()
{
   return maritalStatusRepository.GetAll();
}

Repository:

HefContext db = new HefContext();

public IEnumerable<MaritalStatus> GetAll()
{
   return db.MaritalStatuses;
}

Model class:

public class MaritalStatus
{
   public int Id { get; set; }
   public string Name { get; set; }
   public bool IsActive { get; set; }
}


You may have renamed the table in your database, but your Model class is still tied to the old name. You need to map the entity to the desired table name in the OnModelCreating method of your DbContext object

public class HefContext : DbContext
{
   public DbSet<Bank> Banks { get; set; }
   public DbSet<AccountType> AccountTypes { get; set; }
   public DbSet<MaritalStatus> MaritalStatuses { get; set; }
   protected override void OnModelCreating(DbModelBuilder modelBuilder)
   {
       modelBuilder.Entity<MaritalStatus>().ToTable("MaritalStatuses");
   }
}

Update:

Apparently EF code first has some issues with the pluralization of some table names, such as Status. Out of curiosity I tested a few others and found several which also had this same issue.


I guess this is wrong:

public DbSet<MaritalStatus> MaritalStatuses { get; set; }

Should be:

public DbSet<MaritalStatuses> MaritalStatuses { get; set; }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜