Table-per-Hierarchy and Many to Many relationship

this may seem very easy to solve, but the complication I'm having is that I have a table per hierarchy to store all of the entities and I'm not able to create the relationships over the same table. Here is what I have in the DB and classes:

I have just one table named BaseObject with an ID, name and type. I will create two classes for those entities stored in there. Master and Component. The type column is the discriminator. I have another table to store the relationships between both: A master can have many components and a component also can have many other components.

This is the code I have for the classes:

public partial class BaseObject
{
    public BaseObject()
    {

    }

    public System.Guid ID { get; set; }
    public string Name { get; set; }
    public Nullable<int> Type { get; set; }
}

public class MasterObject : BaseObject
{
    public virtual ICollection<ComponentObject> Components { get; set; }

    public MasterObject()
    {
        this.Components = new List<ComponentObject>();
    }
}

public class ComponentObject : BaseObject
{
    public virtual ICollection<MasterObject> MasterObjects { get; set; }

    public ComponentObject()
    {
        this.MasterObjects = new List<MasterObject>();
    }
}

And these are the mappings:

public class BaseObjectMap : EntityTypeConfiguration<BaseObject>
{
    public BaseObjectMap()
    {
        // Primary Key
        this.HasKey(t => t.ID);

        // Table & Column Mappings
        this.ToTable("BaseObject");
        this.Property(t => t.ID).HasColumnName("ID");
        this.Property(t => t.Name).HasColumnName("Name");

        //configure the inheritance in here
        this.Map<MasterObject>(m => m.Requires("Type").HasValue(1));
        this.Map<ComponentObject>(m => m.Requires("Type").HasValue(2));
    }
}

public class MasterObjectMap : EntityTypeConfiguration<MasterObject>
{
    public MasterObjectMap()
    {
        this.HasMany<ComponentObject>(t => t.Components)
            .WithMany(t => t.MasterObjects)
            .Map(c =>
            {
                c.ToTable("ObjectComponents");
                c.MapLeftKey("ComponentObjectID");
                c.MapRightKey("BaseObjectID");
            });
    }
}

public class ComponentObjectMap : EntityTypeConfiguration<ComponentObject>
{
    public ComponentObjectMap()
    {
        this.HasMany<MasterObject>(t => t.MasterObjects)
            .WithMany(t => t.Components)
            .Map(m =>
            {
                m.ToTable("ObjectComponents");
                m.MapLeftKey("BaseObjectID");
                m.MapRightKey("ComponentObjectID");
            });
    }
}

The thing is that when quering the DB, I can get a Master by accessing the DBSet Masters, but the Component collection always gives a non-sense exception saying that "Problem in mapping fragments starting at line 6:Condition member 'BaseObject.Type' with a condition other than 'IsNull=False' is mapped. Either remove the condition on BaseObject.Type or remove it from the mapping."

I don't understand what's happening. Of course if the classes where pointing each to a table, this would be very easy, but I suspect that is the root of my problem.

Also, I'm just starting with EF. I wanted to create the classes based on my existing DB that I wouldn't like to modify at all. Unless it's really needed. Please guide me if what I'm trying to do is right or wrong, or what should I do first to fully implement EF on this project that is currently using NHibernate.

Any help here? Thanks

Answers


Finally I found the issue thanks to this answer: EF4.1 Exception creating Database with table-per-hierarchy inheritance

The problem is in the mapping of the Base class. When using a base class and a discriminator to get the children classes, the discriminator MUST NOT BE a property in neither instance. Just removed the discriminator as property and it worked fine. In my example, column 'Type' is the discriminator.

Hope this helps to someone else.


Need Your Help

SQL Server 2008 R2 Express - Instances continually starting up database

sql sql-server-2008 restart sql-server-2008r2-express

I have 3 instances running of SQL Server 2008 R2 Express, to try and circumvent the 1gb limit for express editions. Instance 1 (SQLExpress) is attached to my busiest web site, servicing around 3000

Reset Isolation level when using Transaction Scop

c# transactions transactionscope

We're setting the isolation level to Read Uncommitted as shown below.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.