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 7562577
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T13:26:31+00:00 2026-05-30T13:26:31+00:00

I’m working with an application and data structure built upon ASP/ADO.NET and I’m converting

  • 0

I’m working with an application and data structure built upon ASP/ADO.NET and I’m converting part of it to ASP.NET MVC. In the data structure, there exists a “optional one-to-one” relationship, where both tables use the same primary key, and name. Basically this table can be considered an “optional extension” of the primary table. Here are samples of the model:

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; }
}

Obviously, EF 4.1 code first has an issue mapping this automatically. So I realize I must specify the mapping explicitly. I tried this:

    modelBuilder.Entity<ZoneMedia>()
        .HasOptional(zm => zm.ZoneMediaText);

    modelBuilder.Entity<ZoneMediaText>()
        .HasRequired(zmt => zmt.ZoneMedia)
        .WithRequiredDependent(zm => zm.ZoneMediaText)
        .Map(m => m.MapKey("ZoneMediaID"));

But it is still giving me an exception about the name of the primary key.

Schema specified is not valid. Errors: 
(199,6) : error 0019: Each property name in a type must be unique. Property name     'ZoneMediaID' was already defined.

I’m a little stumped. I need to adapt to this non-conventional structure I realize in EF 4.1 it would be much easier to just add a unique PK to the optional relation and hold the foreign key relationship in the primary table, but I can’t change the database layout. Any advice would be appreciated.

  • 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-30T13:26:32+00:00Added an answer on May 30, 2026 at 1:26 pm

    I hope i understood well.

    This works for me:

    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();
        }
    }
    

    This Correctly create a FK_ZoneMediaTexts_ZoneMedias_ZoneMediaID in ZomeMediaTexts table, and the Foreign Key is the Primary Key.

    EDIT: maybe it’s worth pointing out that I’m using EF 4.3.0

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have an MVC Razor view @{ ViewBag.Title = Index; var c = (char)146;
I want to construct a data frame in an Rcpp function, but when I
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
I have some data like this: 1 2 3 4 5 9 2 6
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but

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.