从数据库更新DC.edmx模型后读取Users表数据报错,求解析与修复方案
Hey there! Let’s dig into the error you’re getting when reading the Users table after updating your DC.edmx model. I’ve dealt with plenty of these EDMX sync issues before, so here’s what’s likely going on and how to fix it.
First, Let’s Break Down the Likely Error Types
1. "Entity type 'Users' has no key defined" or Primary Key-related Errors
What this means: After updating the EDMX, the primary key for your
Userstable either wasn’t synced correctly, or the database’s primary key was modified but the model didn’t pick up the change. EF needs a unique key to identify entities, so this breaks read operations.
2. "No mapping metadata found for type 'DC.Users'"
What this means: The mapping between your
Usersentity in the EDMX and the actual database table is broken. This usually happens if field names/types changed in the database but the model update didn’t fully sync, or the table was renamed without updating the EDMX.
3. "Property/Field 'XXX' does not exist in model/database"
What this means: Your EDMX model and the
Userstable in the database have mismatched structures—maybe you added/deleted a field in the database, but the EDMX update missed it, causing EF to look for something that doesn’t exist (or vice versa).
Practical Fixes to Try
Re-sync the EDMX properly and validate mappings
- Right-click your DC.edmx file and select Update Model from Database
- In the update wizard, make sure the
Userstable is checked, and choose the Refresh option (not just "Add") to overwrite outdated mappings - Open the EDMX design view: check that the
Usersentity has a key icon next to its primary field, and that all field names/types match exactly what’s in your database - Right-click your project and select Build to make sure there are no compilation errors
Manually fix entity mappings (if auto-update fails)
Sometimes the auto-update glitches—here’s how to fix it manually:- Right-click the EDMX file → Open With → XML Editor
- Find the
<EntityType Name="Users">node, then confirm the<Key>section points to the correct primary key, like this:<Key> <PropertyRef Name="UserId" /> </Key> - Double-check each
<Property>node’sTypeandNullableattributes match your database (e.g., if yourEmailfield isvarchar(255) NOT NULL, the model should have<Property Name="Email" Type="varchar" MaxLength="255" Nullable="false" />)
Clear EF context and project cache
Old cached model data can cause weird errors:- Delete your project’s
binandobjfolders, then rebuild the project - If this is a web app, restart your IIS server or app pool to clear application-level cache
- Delete your project’s
Verify your connection string
Double-check yourapp.config/web.configto make sure the connection string points to the correct database (not an old backup or different environment) and that the database version is compatible with your EDMX model.Audit the actual
Userstable in the database
Sometimes the issue is on the database side:- Confirm the primary key is still intact and hasn’t been modified
- Check that all fields match the EDMX in terms of data type, length, and nullability
- If
Usershas foreign key relationships, make sure those linked tables are also present and correctly mapped in the EDMX
If you can share the exact error message from your screenshot, we can narrow this down even further—but these steps should fix most common EDMX sync issues with reading table data.
内容的提问来源于stack exchange,提问作者John Haga

