【问题标题】:Entity Framework - cannot be inferred from usage实体框架 - 无法从使用情况推断
【发布时间】:2013-05-26 16:12:36
【问题描述】:

我一直在尝试先学习 EF 代码。首先要做的事情之一是它不会强制执行唯一性......所以......我试图通过公开一个只读的 IEnumerble 属性来解决这个问题,如果我想向收藏...

当我尝试这样做时(这只是下面的“扔掉”示例)我得到了错误。

错误 1 ​​无法从用法中推断方法“System.Data.Entity.ModelConfiguration.EntityTypeConfiguration.HasMany(System.Linq.Expressions.Expression>>)”的类型参数。尝试明确指定类型参数。 C:\Users\User\Documents\Visual Studio 2010\Projects\ConsoleApplication3\ConsoleApplication3\Program.cs 39 9 ConsoleApplication3

有什么原因吗?

class Program
{
  static void Main(string[] args)
  {
    using (DC _db = new DC())
    {
      PrimeA p = new PrimeA { Name = "BlahGEEEER" };
      p.AddProp(new Prop { comment = "Blah HI!" });
      p.AddProp(new Prop { comment = "Blah HI!" });


      Console.ReadLine();
      _db.PrimeAs.Add(p);
      _db.SaveChanges();
    }
  }
}

public class DC : DbContext
{
  public DbSet<PrimeA> PrimeAs { get; set; }
  public DbSet<PrimeB> PrimeBs { get; set; }
  public DbSet<Prop> Props { get; set; }
  protected override void OnModelCreating(DbModelBuilder modelBuilder)
  {
    modelBuilder.Entity<PrimeA>()
      .HasMany(p => p.Props)  // <---- FAILS HERE
      .WithMany();

    base.OnModelCreating(modelBuilder);
  }
}

public class PrimeA
{

  private List<Prop> m_Props = new List<Prop>();

  public int PrimeAID { get; set; }
  public string Name { get; set; }
  public virtual IEnumerable<Prop> Props
  {
    get 
    {
      return m_Props;
    }

  }

  public bool AddProp(Prop prop)
  {
    bool ret = false;
    var existingResult =
      from p in m_Props
      where p.comment.ToLower() == prop.comment.ToLower()
      select p;
    if (existingResult.Count() == 0)
    {
      m_Props.Add(prop);
      ret = true;
    }
    return ret;
  }
}

【问题讨论】:

    标签: c# entity-framework


    【解决方案1】:

    泛型函数具有类型参数,它们会尝试“猜测”/“推断”类型参数,但有时会混淆,您必须明确指定它们。我不知道在这种情况下它无法推断的原因,但在你的鞋子里我会尝试类似的东西,因为在这种情况下我认为它想知道目标集合的类型。

    .HasMany<Prop>(p => p.Props) 
    

    【讨论】:

      【解决方案2】:

      正如您在 MSDN 中看到的那样,EntityTypeConfiguration.HasMany 需要 ICollection&lt;TTargetEntity&gt;。所以你必须在

      中更改Props
      public virtual ICollection<Prop> Props
      

      【讨论】:

      • 另外,通常我也看不到使用私有支持字段,因为 EF 将使用虚拟属性在它生成的代理对象中生成替换实现。不要使用私有支持字段,而是使用自动{ get;set;} 并在构造函数中初始化列表而不是私有字段。我不确定这是否有必要,但这是基于我通常看到/做过的事情。
      • 我故意没有完成属性的定义。我认为 OP 希望确保它永远不会是 null
      • “我认为 OP 希望确保它永远不会为空”,是的,我只是建议他们在构造函数中这样做,放弃支持私有字段,并使用自动 getter/setter。并不是要批评您的答案,而是对 OP 的额外建议。由于无法进行测试,我只是随意推测,由于虚拟代理的工作方式,它们在使用私有支持字段时可能无法获得预期的行为。
      • 我试图禁止用户直接向集合中输入任何内容。看起来我将不得不做的越来越多的是添加一个额外的抽象层,这样用户甚至看不到数据库端。我希望在不必做 DAL / BL 场景的情况下让这个简单......但看起来这不是一个选择。
      【解决方案3】:

      尝试对 Props 属性使用 ICollection 而不是 IEnumerable。这应该会使错误消失。

      这里有几篇文章有助于解释为什么要使用 IList 或 ICollection 而不是 IEnumerable。

      ICollection Vs List in Entity Framework

      Why does the entity framework need an ICollection for lazy loading?

      我还建议对 Props 的私有属性使用 HashSet 而不是 List

      【讨论】:

        猜你喜欢
        • 2020-12-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-03-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多