EF 4.1, inheritance and joint primary key association => ResultType of the specified expression is incompatible

Summary

I have three classes:

  • Account
  • SpecialAccount(inherits from Account)
  • Profile(ratio 0..1 to SpecialAccount)

In other words, a SpecialAccountmay have 0 or 1 Profiles. A Profilemust have SpecialAccount.

In EF, this can only be configured as a shared primary key relationship.

When you request Profileand ask about materials from SpecialAccount(for example, "find profiles, where profile.SpecialAccount.Name == "blah"), I get this error:

{"The ResultType of the specified expression is incompatible with the required type.
The ResultType expression is" Transient.reference [EFInheritanceTest.Account] ", but the
required type is" Transient.reference [EFInheritanceTest.SpecialAccount] ".
\ R \ nParameter: arguments 1 "}

More details

This code illustrates the problem:

namespace EFInheritanceTest
{
  class Program
  {
      static void Main(string[] args)
      {
         using (var context = new MyContext())
         {
            var t = context.Profiles.Where(p => p.SpecialAccount.Name == "Fred");
            Console.WriteLine(t.Count());

            Console.ReadKey();
         }
      }
  }

  public class MyContext : DbContext
  {
     public DbSet<Account> Accounts { get; set; }
     public DbSet<SpecialAccount> SpecialAccounts { get; set; }
     public DbSet<Profile> Profiles { get; set; }

     protected override void OnModelCreating(DbModelBuilder modelBuilder)
     {
         base.OnModelCreating(modelBuilder);

         modelBuilder.Entity<SpecialAccount>().HasOptional(a => a.Profile);
         modelBuilder.Entity<Profile>().HasRequired(p => p.SpecialAccount);
     }
  }

 public class Account
 {
     public int ID { get; set; }
     public string Name { get; set; }
 }

 public class SpecialAccount : Account
 {
      public virtual Profile Profile { get; set; }
 }

 public class Profile
 {
     public int ID { get; set; }
     public string Summary { get; set; }
     public virtual SpecialAccount SpecialAccount { get; set; }
 }
}

Research so far

In principle, the culprit appears to be a common association of primary keys; When Profilelooking for it SpecialAccount, gets the parent object instead Account.

The only solution I see is to change it like this;

public class SpecialAccount : Account
{
    public virtual ICollection<Profile> Profiles { get; set; }
}

and maintain rules in code, rather than use a database. But it is just ugly.

question Connect - ??

, EF4.1, - - - , .

+5
1

, , , , :

var t = from p in context.Profiles
        join s in context.SpecialAccounts
          on p.ID equals s.ID
        where s.Name == "Fred"
        select p;
var count = t.Count();

:

var t = context.Profiles
               .Join(context.SpecialAccounts,
                     p => p.ID,
                     s => s.ID,
                     (p, s) => new { s, p })
               .Where(r => r.s.Name == "Fred");
var count = t.Count();

, , , . ( EF 4.1)

+4

All Articles