Summary
I have three classes:
AccountSpecialAccount(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, - - - , .