entityentity-framework-4entity-relationshipcode-first

entity framework 4, code-only, relationships


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

        }    
    }       
}

Solution

  • 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/