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

  • Home
  • SEARCH
  • 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 7548547
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T09:43:34+00:00 2026-05-30T09:43:34+00:00

In my AspNet MVC 3 project when I try to scaffold an entity which

  • 0

In my AspNet MVC 3 project when I try to scaffold an entity which has a One to Zero or One relationship with another entity I get “An item with the same index has already been added” error.

Essentially this happens when the Primary Key of the related table is also a Foreign Key.

At the moment my workaround is

  1. Add an Id column to the related table and make it the primary key

  2. Add Unique Key to the Foreign Key Column.

The problem with this is that EF will generate an ICollection navigation property for the related entity instead of just a property of the related entity type (which I can set to null in case of zero related entities)

Is this a know bug?

Am I doing something wrong?

Is there a better work around to get rid of the ICollection navigation property?

  • 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-05-30T09:43:35+00:00Added an answer on May 30, 2026 at 9:43 am

    See my answer on this question:

    How do I code an optional one-to-one relationship in EF 4.1 code first with lazy loading and the same primary key on both tables?

    That’s the example code with the correct configuration.

    public class ZoneMedia
    {
        public int ZoneMediaID { get; set; }
        public string MediaName { get; set; }
        public int Width { get; set; }
        public int Height { get; set; }
    
        public virtual ZoneMediaText MediaText { get; set; }
    }
    
    public class ZoneMediaText
    {
        public int ZoneMediaID { get; set; }
        public string Text { get; set; }
        public int Color { get; set; }
    
        public virtual ZoneMedia ZoneMedia { get; set; }
    }
    
    public class TestEFDbContext : DbContext
    {
        public DbSet<ZoneMedia> ZoneMedia { get; set; }
        public DbSet<ZoneMediaText> ZoneMediaText { get; set; }
    
        protected override void OnModelCreating (DbModelBuilder modelBuilder)
        {
            modelBuilder.Entity<ZoneMedia>()
                .HasOptional(zm => zm.MediaText);
            modelBuilder.Entity<ZoneMediaText>()
                .HasKey(zmt => zmt.ZoneMediaID);
            modelBuilder.Entity<ZoneMediaText>()
                .HasRequired(zmt => zmt.ZoneMedia)
                .WithRequiredDependent(zm => zm.MediaText);
    
            base.OnModelCreating(modelBuilder);
        }
    }
    class Program
    {
        static void Main (string[] args)
        {
            var dbcontext = new TestEFDbContext();
            var medias = dbcontext.ZoneMedia.ToList();
        }
    }
    

    You can also achieve this with DataAnnotations, but I generally prefer to keep my entity models as POCOs.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an embedded database in an asp.net mvc project. If I try to
I have an ASPNET MVC 2 project. When I use <%= Html.TextBoxFor(model => model.Login)
I am about to start a project for a potentially heavily accessed ASPNET MVC
Once an asp.net MVC project has a web deployment project associated with it, there
I try to run ELMAH with my ASP.NET MVC 2 project but have some
When I try to create a new project, and go to web, no mvc
I want to use Application_Error with my MVC project, but i can't get it
I'm working on an ASP.Net MVC 3 project, which needs localized error messages from
When I try sample application which is made as default with asp.net mvc 3.0,
I have to try to use internatinalization with this example. http://afana.me/post/aspnet-mvc-internationalization-part-2.aspx But I obtain

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.