Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8273585
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T07:31:36+00:00 2026-06-08T07:31:36+00:00

I have built out my models using POCO. When I go to seed my

  • 0

I have built out my models using POCO. When I go to seed my database I get:

Unable to determine the principal end of the ‘CSP.Models.Type_Color’ relationship. Multiple added entities may have the same primary key

Here’s the models in question:

public class Type
{
    [Key]
    [ScaffoldColumn(false)]
    public int TypeId { get; set; }

    [Required(ErrorMessage = "A Type Name is Required")]
    [Display(Name="Type")]
    public string Name { get; set; }

    public int ColorId { get; set; }
    public bool Other { get; set; }

    //Navigations
    [ForeignKey("ColorId")]
    public virtual Color Color { get; set; }
    public virtual List<Tools> tools { get; set; }

}

public class Color
{
    [Key]
    [ScaffoldColumn(false)]
    public int ColorId { get; set; }

    [Required(ErrorMessage = "A name is required")]
    public string Name { get; set; }

    public string Description { get; set; }

    //navigation
    public virtual List<Type> Types { get; set; }
}

Most of the markup I did after reading suggestions.

My seed code that is getting the error is here:

var colors = new List<Color>
        {
            new Color{Name="Red"},
            new Color{Name="White"}
        };

            var types = new List<Type>
        {
            new Type{ Name="Hammer", Color = colors.Where(ws => ws.Name=="Red").Single()},
            new Type{ Name= "Electric", Color = colors.Where(ws => ws.Name=="Red").Single()}
        };

new List<Tool>
        {
            new Wine{ Maker= Maker.Single(v => v.Name=="HammerCo"), Type= types.Single(wt => wt.Name=="hammer")},
        }
        }.ForEach(a => context.Tools.Add(a));
            context.SaveChanges();

I also tried adding each value to the context and then saving. I got this error after it tried saving the type entity:

[System.Data.SqlClient.SqlException] = {“The INSERT statement conflicted with the FOREIGN KEY constraint \”Type_Color\”. The conflict occurred in database \”TestTools\”, table \”dbo.Colors\”, column ‘ColorId’.\r\nThe statement has been terminated.”}

What am I missing?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T07:31:37+00:00Added an answer on June 8, 2026 at 7:31 am

    What is happening is your objects all have the default int value (0) for their primary key. When you add them to the context, EF detects this and throws an error (two objects of the same type cannot have the same key, in this case, 0. I assume your primary key fields in the database are set as IDENTITY columns and will auto increment +1 on insert. This may sound odd, but you need to give your objects placeholder IDs which will be replaced on insert with the IDENTITY values.

    new Color{ColorId = 1, Name="Red"},
    new Color{ColorId = 2, Name="White"}
    new Type{TypeId = 1, Name="Hammer", ...}
    new Type(TypeId = 2, Name="Electric", ...}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have built a local DVD Database using Codeigniter, With film names etc in.
We have an application that is built with Excel as the front end using
I have built part of my app and wanted to test it out. All
Are there any scripts out there, or have any of you built a tool,
I have built a mock object using EasyMock, and I'm trying to have the
I have a web application built in asp.net mvc using forms authentication. I am
Now that I have built a database. Visual Studio 2008 SP1’s ADO.NET Entity Framework
I have built a WP7.1 application that uses a local database. I used sqlmetal
We have been using the Weka Explorer GUI to build a few classifier models.
I have successfully implemented login with Facebook using Devise and OmniAuth (built into Devise).

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.