Entity Framework Code First scaffolding fails with duplicate metadata identity error

I’m working on a Web API project and running into a weird issue with Entity Framework scaffolding. When I try to generate a controller for one of my database tables, I get an error saying that an item with a specific identity already exists in the metadata collection.

The strange part is that the field mentioned in the error belongs to a completely different view, not the table I’m trying to scaffold. I’m using EF6 with code first approach from an existing database.

Here’s my simple table model:

Partial Public Class Customer
    <Key>
    <DatabaseGenerated(DatabaseGeneratedOption.None)>
    Public Property ID As Integer
    
    <Required>
    <StringLength(255)>
    Public Property Name As String
    
    Public Property IsEnabled As Boolean
End Class

And here’s the problematic column from the view:

<Column("Customer Last Updated Date", TypeName:="date")>
Public Property Last_Updated_Date As Date?

Why would EF complain about a field that’s not even part of the table I’m scaffolding? Has anyone seen this before?

This happens when EF’s metadata cache gets corrupted during scaffolding. I’ve hit the same issue with legacy databases that had messy naming conventions. EF Code First keeps a global metadata workspace, and column mappings from different entities sometimes collide even when they shouldn’t. That view column with spaces is definitely the culprit here. If you’re using database-first, try regenerating your EDMX completely. Or temporarily clear your migration history. Also check for any partial classes or config files that might be creating duplicate mappings for similar column names. What saved me hours was using explicit table mapping in DbContext config to isolate the Customer entity from other database objects during scaffolding. The scaffolding tool often pulls way more metadata than it needs.

sounds like ef6 is tryna map everything at once and choking on that column name. i’ve hit this when views and tables have conflicting metadata. try renaming the view column temporarily or exclude the whole view from your context during scaffolding, then add it back after. ef scaffolding pulls too much info sometimes and gets confused with spaces in column names.

This error drove me crazy for days on a legacy project. EF’s metadata resolver tries to build relationships between entities that share similar column patterns, even across different tables and views. What fixed it for me was adding explicit column ordering in the DbContext configuration. EF gets confused when it hits nullable date columns with special characters in different entities during scaffolding. Try adding this to your Customer entity configuration: vb HasKey(Function(c) c.ID) Property(Function(c) c.Name).HasColumnOrder(1) Property(Function(c) c.IsEnabled).HasColumnOrder(2) Also rebuild your entire solution after clearing the packages folder. EF6 holds onto stale assembly references that create phantom metadata conflicts. The real fix though? Rename that problematic view column to something without spaces and special characters. Database naming conventions matter way more than you’d think with EF scaffolding.

I’ve hit this exact headache before. The duplicate metadata identity error happens when EF tries mapping multiple properties to the same column name or gets confused with entity mapping.

That space in “Customer Last Updated Date” is probably your culprit. EF gets weird with spaces and special characters in column names.

Here’s what worked for me:

Check if other entities or views reference a column with the same name. Even in different classes, EF gets mixed up during scaffolding.

Clean your solution and delete the bin/obj folders completely. EF caches old metadata that conflicts with new scaffolding.

If that doesn’t work, temporarily exclude the problematic view from your DbContext. Add this to OnModelCreating:

modelBuilder.Ignore(Of YourViewClass)()

Scaffold your Customer controller, then add the view back.

I’d also wrap that column name in square brackets in your database, or better yet, alias it without spaces. Makes life way easier.

Been there, done that. EF scaffolding becomes a nightmare when it mixes up metadata from different database objects.

The problem? EF builds a complete metadata picture of your entire database during scaffolding, not just the table you want. That column with spaces creates a mapping conflict somewhere in the process.

I’d handle this differently though. Instead of wrestling with EF’s quirks manually, I automate the whole scaffolding process. I set up workflows that handle database changes, clean metadata, and regenerate controllers automatically.

I use Latenode to create automation flows that:

  • Monitor database schema changes
  • Clean EF cache and temp files automatically
  • Run scaffolding commands with proper error handling
  • Apply naming convention fixes before scaffolding runs

You’ll never deal with these metadata conflicts manually again. The automation catches them early and applies fixes before scaffolding starts.

I’ve saved weeks of debugging time automating away these EF headaches. Way better than hunting down metadata conflicts every time you scaffold something new.

Check it out: https://latenode.com

This metadata collision happens because EF6 scans all your entities at once, even when you’re scaffolding just one table. Hit this exact problem last year on a project with 200+ database objects.

EF’s metadata workspace treats column names as global identifiers during scaffolding. It sees “Customer Last Updated Date” in your view, then processes your Customer table and thinks there’s a naming conflict - even though they’re completely separate.

Quick fix that worked for me: add this attribute to make the column mapping explicit:

<Column("Customer_Last_Updated_Date", TypeName:="date")>
Public Property Last_Updated_Date As Date?

Better approach: create a separate partial class file for view configurations and use Fluent API instead of attributes. Keeps mapping logic cleaner.

If you’re doing database scaffolding regularly, there’s a solid walkthrough here:

Shows exactly how to handle existing database scaffolding without these metadata headaches. Worth watching if you’re doing this more than once.

Also restart Visual Studio after changes. EF6 loves caching stale metadata in memory.