DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Beyond Partitioning and Z-Order: A Deep Dive into Liquid Clustering for Unity Catalog Managed Tables
  • The Quantum Computing Mirage: What Three Years of Broken Promises Have Taught Me
  • Five Nonprofit & Charity APIs That Make Due Diligence Way Less Painful for Developers
  • Anthropic’s Model Context Protocol (MCP): A Developer’s Guide to Long-Context LLM Integration

Trending

  • Architecting Proactive IT: NinjaOne Remote Monitoring and Management
  • The Big Data Architecture Blueprint: Core Storage, Integration, and Governance Patterns
  • Observability for Agents and Workflows: Tracing Prompts, Tool Calls, and Business Outcomes End-to-End
  • Deployment Lessons You Only Learn the Hard Way
  1. DZone
  2. Data Engineering
  3. Data
  4. Short Walks: System.InvalidOperationException — How to Deal With It [Snippet]

Short Walks: System.InvalidOperationException — How to Deal With It [Snippet]

Here's how to address a little-known issue — "The seed entity for entity type 'MyEntity' cannot be added..." — with EF Core 2.

By 
Paul Michaels user avatar
Paul Michaels
·
Jul. 12, 18 · Code Snippet
Likes (1)
Comment
Save
Tweet
Share
8.0K Views

Join the DZone community and get the full member experience.

Join For Free

I got this error recently while playing with EF Core 2. There's very little on Google about it, and although it's not a hugely difficult problem to solve, if I ever get it again, I can just Google it!

The error:

System.InvalidOperationException: "The seed entity for entity type 'MyEntity' cannot be added because another seed entity with the same key value for {'Id'} has already been added. Consider using 'DbContextOptionsBuilder.EnableSensitiveDataLogging' to see the conflicting key values."

It is effectively caused by a conflict in the primary key, and it gives you the first step towards solving it in the error (in OnModelCreating):

var options =
    new DbContextOptionsBuilder<ApplicationDbContext>()
         .UseSqlServer(configuration.GetConnectionString("DefaultConnection"))
         .EnableSensitiveDataLogging()
         .Options;


Now it says:

System.InvalidOperationException: "The seed entity for entity type 'MyEntity' cannot be added because another seed entity with the key value 'Id:1′ has already been added."

In my particular case, I'd been playing around with adding some seed data, and had left this in (OnModelCreating):

modelBuilder.Entity<MyEntity>().HasData(new Data.MyEntity()
{
    Id = 1,
    Name = "Test",
    CreatedDate = new DateTime(2018, 07, 03),
    UpdatedDate = new DateTime(2018, 07, 03)
});
entity Data Types Google (verb) Data (computing) Entity Framework IT

Published at DZone with permission of Paul Michaels. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Beyond Partitioning and Z-Order: A Deep Dive into Liquid Clustering for Unity Catalog Managed Tables
  • The Quantum Computing Mirage: What Three Years of Broken Promises Have Taught Me
  • Five Nonprofit & Charity APIs That Make Due Diligence Way Less Painful for Developers
  • Anthropic’s Model Context Protocol (MCP): A Developer’s Guide to Long-Context LLM Integration

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook