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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T13:47:18+00:00 2026-05-15T13:47:18+00:00

I can’t figure out how to solve the following problem. What i need it

  • 0

I can’t figure out how to solve the following problem.
What i need it a relationship from one base class to another, so that every derived class has a relationship with the same table, called ‘Item’ in my example.

Since this is just an example it doesn’t reflect my program. In the real program the relationship with class Item is in a different namespace. Therefore it can’t be in the derived class.

The error:

A key is registered for the derived type ‘WebApplication1.Client’. Keys must be registered for the root type ‘WebApplication1.Base’.

namespace WebApplication1
{
    public class Item
    {
        public int ItemID { get; set; }
    }

    public class Base
    {
        public int ID { get; set; }
        public int ItemID { get; set; }

        public Item Item { get; set; }

    }

    public class Client : Base
    {
        public string Name { get; set; }

        private List<Project> _projects = null;

        public List<Project> Projects
        {
            get
            {
                if (_projects == null)
                    _projects = new List<Project>();

                return _projects;
            }
        }
    }

    public class Project : Base
    {
        public string Name { get; set; }

        public int ClientId { get; set; }

        public Client Client { get; set; }

    }

    public class Main
    {
        public static void Test()
        {
            ContextBuilder<ObjectContext> ContextBuilder = new ContextBuilder<ObjectContext>();

            var itemConfig = new EntityConfiguration<Item>();
            itemConfig.HasKey(p => p.ItemID);
            itemConfig.Property(p => p.ItemID).IsIdentity();
            ContextBuilder.Configurations.Add(itemConfig);

            var clientConfig = new EntityConfiguration<Client>();
            clientConfig.HasKey(p => p.ID);
            clientConfig.Property(p => p.ID).IsIdentity();
            clientConfig.Property(p => p.Name);
            clientConfig.Relationship(p => p.Item).HasConstraint((p, c) => p.ItemID == c.ItemID);           
            ContextBuilder.Configurations.Add(clientConfig);

            var projectConfig = new EntityConfiguration<Project>();
            projectConfig.HasKey(p => p.ID);
            projectConfig.Property(p => p.ID).IsIdentity();
            projectConfig.Property(p => p.Name);

            projectConfig.Relationship(p => p.Item).HasConstraint((p, c) => p.ItemID == c.ItemID);

            projectConfig.Relationship(p => p.Client).FromProperty(p => p.Projects).HasConstraint((p, c) => p.ClientId == c.ID);

            ObjectContext objCtx = ContextBuilder.Create(new SqlConnection(@"Data Source=(local);Initial Catalog=testa;Integrated Security=SSPI;"));

            if (!objCtx.DatabaseExists())
                objCtx.CreateDatabase();

        }    
    }       
}
  • 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-15T13:47:19+00:00Added an answer on May 15, 2026 at 1:47 pm

    Look at how do deal with inheritance mapping here: Link

    For basic non-relational proberties and how-to reuse them: https://danielwertheim.wordpress.com/2009/11/29/entity-framework-4-how-to-reuse-mappings-and-add-a-concurrency-token/

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

Sidebar

Related Questions

Can I figure out if a function has already been assigned to an event?
Can't figure out how to do this in a pretty way : I have
Can anyone tell me whether Helvetica is a browser base font? If so, it
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
Can anyone give me an example of how to return the following json simply
Can anybody tell what is the best(easy) way to sort the following string 'index'
Can I register a .Net COM class with the SingleUse -flag? The reason I
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
Can some one Guide me to work with these things... What is Model popup
Can't work out a way to make an array of buttons in android. This

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.