Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/flutter/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Entity framework EF 4.1,继承和共享主键关联=>;指定表达式的ResultType不兼容_Entity Framework_Entity Framework 4.1_Ef Code First - Fatal编程技术网

Entity framework EF 4.1,继承和共享主键关联=>;指定表达式的ResultType不兼容

Entity framework EF 4.1,继承和共享主键关联=>;指定表达式的ResultType不兼容,entity-framework,entity-framework-4.1,ef-code-first,Entity Framework,Entity Framework 4.1,Ef Code First,摘要 我有三门课: 账户 SpecialAccount(继承自账户) Profile(0..1与SpecialAccount的关系) 换句话说,SpecialAccount可以有0或1个配置文件。Profile必须具有SpecialAccount 在EF中,只能将其设置为共享主键关系 当查询profile并询问SpecialAccount中的内容时(例如,“查找profiles whereprofile.SpecialAccount.Name==“blah”),我会遇到以下错误: {“指定表

摘要

我有三门课:

  • 账户
  • SpecialAccount
    (继承自
    账户
  • Profile
    (0..1与
    SpecialAccount
    的关系)
换句话说,
SpecialAccount
可以有0或1个
配置文件。
Profile
必须具有
SpecialAccount

在EF中,只能将其设置为共享主键关系

当查询
profile
并询问
SpecialAccount
中的内容时(例如,“查找profiles where
profile.SpecialAccount.Name==“blah”
),我会遇到以下错误:

{“指定表达式的ResultType与所需类型不兼容。
表达式结果类型为“Transient.reference[EFInheritanceTest.Account]”,但
所需类型为“Transient.reference[EFInheritanceTest.SpecialAccount]”。
\r\n参数名称:参数“}

详细信息

此代码说明了问题:

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; }
 }
}
并在代码中维护规则,而不是使用数据库。但这很难看

我在Connect上找到了这个相关的和-但它已被标记为已解决


我怀疑这是EF4.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();

这不是很好,但您的原始查询不起作用的事实在我看来确实像是一个bug。(我已经用EF 4.1进行了测试)

我们也遇到了类似的问题;至少我们收到了相同的错误消息。升级到.Net Framework 4.5(从4.0)为我们修复了EF 4.4的问题
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();