ASP.NET MVC中使用EF Code-First重建误删Event表求助
Hey there, let's work through this issue together. You deleted the Event table manually and now can't get EF Code-First to rebuild it, hitting that permission/table missing error. Here's how to fix this step by step:
First, Rule Out Actual Permission Issues
Before diving into EF specifics, let's confirm your database user has the right access:
- Open SQL Server Management Studio (or your preferred DB tool) and log in using the same credentials from your app's connection string.
- Try manually creating a test table (e.g.,
CREATE TABLE Test (Id INT PRIMARY KEY);). If this fails, you need to grant your user DDL permissions—like adding them to thedb_ddladminrole in SQL Server, or explicitly grantingCREATE TABLEpermissions. - If the test table creates successfully, the issue is with EF's migration state, not raw database permissions.
Fix EF Migration Mismatch
Code-First relies on the __MigrationHistory table to track schema changes. When you deleted the Event table manually, EF still thinks the table exists in the database. Here's how to reset this:
Option 1: Reset Migrations Without Losing Other Tables
- Open the Package Manager Console (PMC) in Visual Studio (Tools > NuGet Package Manager > Package Manager Console).
- Run this command to create an empty migration that ignores existing schema differences:
Add-Migration InitialCreate -IgnoreChanges - Then update the database to apply this migration (which will trigger EF to create any missing tables, including Event):
Update-Database
Option 2: Full Migration Reset (If You Have No Critical Existing Data)
If you don't mind wiping migration history completely:
- Back up your database first (just in case).
- Delete the Migrations folder from your project.
- In your database, delete the
__MigrationHistorytable. - Re-enable migrations and create a new initial migration:
Enable-Migrations -EnableAutomaticMigrations Add-Migration InitialCreate Update-Database
Clean Up Your Event Entity Class
Looking at your Event class, there are a few inconsistencies that might confuse EF:
- You have a primary key
Id, but alsoEvntID—this is redundant and could cause EF to generate unexpected schema. RemoveEvntIDunless it's a foreign key (which it doesn't look like here). - You have
Department(nullable int) andDepartID—these are likely duplicates. IfDepartIDis meant to be a foreign key to a Department table, structure it like this:
(Same goes forpublic int DepartID { get; set; } [ForeignKey("DepartID")] public virtual Department Department { get; set; }DesignationandDesigID.) - The commented-out
fileproperty (HttpPostedFileBase) shouldn't be in your entity class—EF can't map that to a database column, so keep it out of the model (you're already handling it in the action method, which is correct).
Quick Fix for Your Create Action Method
While not related to the table creation error, your current Create method has a bug: you commented out files.InputStream.CopyTo(stream);, so your Image field will always be an empty byte array. Uncomment that line to actually save the uploaded image:
MemoryStream stream = new MemoryStream(); files.InputStream.CopyTo(stream); // Uncomment this! byte[] image = stream.ToArray(); evnt.Image = image;
Give these steps a try—start with the permission check, then fix the migration state, and clean up the entity class. That should get your Event table back up and running!
内容的提问来源于stack exchange,提问作者Muhamad Taqi

