1
votes

I have a base class from which almost everything else inherits..

public abstract class MyBaseClass
{
   public int Id { get; set; }

   public int OwnerId{ get; set; }
   public virtual Org Owner{ get; set; }

}

public abstract class Party: MyBaseClass
{

}

public class Org: Party
{
   public string Name { get; set; }
   public DateTime CreationDate { get; set; }
}

My data context has a line like this:

public DbSet<Org> Orgs { get; set; }

When executing a query like this...

var orgs = db.Orgs;

I get this error:

Multiplicity is not valid in Role 'Org_Owner_Source' in relationship 'Org_Owner'. Because the Dependent Role properties are not the key properties, the upper bound of the multiplicity of the Dependent Role must be '*'.

In order to overcome this problem, I added the following Fluent API code:

    modelBuilder.Entity<MyBaseClass>()
                .HasRequired(e => e.Owner)
                .WithMany()
                .HasForeignKey(e => e.OwnerId)
                .WillCascadeOnDelete(false);

This resolves the Invalid Multiplicity error but when I subsequently run my query, it returns a "Sequence contains no elements error". When I inspect the generated SQL code that is hitting the database, I find that the query's 'from' clause is referencing a non-existent MyBaseClassClasses table instead of the Orgs table as expected.

Running a migration also throws the 'Sequence contains no elements' error. Anyway, I wouldn't want all the classes that inherit from MyBaseClass to be denormalized into a single MyBaseClassClasses table.

Questions:

  1. Is the fluent api code correct in this situation?
  2. If no, what should it be?
  3. If yes, then how can I overcome the 'Sequence contains no elements' error?

***** Follow-up Edit ******************

The other way I have been trying it is with the Fluid Mapping removed and the following class definitions:

public abstract class MyBaseClass
{
   public int Id { get; set; }

   [ForeignKey("Owner")]
   public int OwnerId{ get; set; }
   public virtual Org Owner{ get; set; }

}

public abstract class Party: MyBaseClass
{

}

public class Org: Party
{
   public string Name { get; set; }
   public DateTime CreationDate { get; set; }
}

This solution throws the "Multiplicity is not valid in Role 'Org_Owner_Source' in relationship 'Org_Owner'. Because the Dependent Role properties are not the key properties, the upper bound of the multiplicity of the Dependent Role must be '*'." error.

I was able to partially work around the problem as I discovered that any other class, besides Org, that inherits from MyBaseClass can have a lazy loading Owner attribute if the attribute is entered directly in to child class and a fluid api entry is made.

For example:

public class MyOtherClass: MyBaseClass
{
   public string Name { get; set; }
   public string Whaterver { get; set; }
   public virtual Org Owner{ get; set; }
}

With the fluid API entry:

modelBuilder.Entity<MyOtherClass>().HasRequired(x => x.Owner).WithMany().HasForeignKey(x=>x.OwnerId).WillCascadeOnDelete(true);
1

1 Answers

2
votes

That is not the correct fluent mapping. By calling modelBuilder.Entity<MyBaseClass>(), you are telling Entity Framework that MyBaseClass is an entity and should somehow map to the database. Instead, use lowest class that you want mapped.

For example:

modelBuilder.Entity<Org>().HasRequired(e => e.Owner);