【问题标题】:Foreign key relationship in EF Code first not returning dataEF代码中的外键关系首先不返回数据
【发布时间】:2013-01-30 10:12:22
【问题描述】:

我希望有人能够帮助我解决问题。

我首先使用实体​​框架代码,但我正在努力处理外键关系。

关系是这样建立的:

public class ExpScen
{
    [Key]
    [DisplayName("Exposure Id")]
    public int ExpId { get; set; }
    [DisplayName("Request Id")]
    [Required(ErrorMessage = "Request Id is required")]
    public int ReqId { get; set; }
    [DisplayName("Quantity")]
    [MaxLength(50, ErrorMessage = "The maximum length for Quantity is 50 characters")]
    public string Quantity { get; set; }
    [DisplayName("No. Exposed")]
    [MaxLength(1, ErrorMessage = "The maximum length for No. Exposed is 1 character")]
    public string Number { get; set; }
    [DisplayName("Categories")]
    public string Categories { get; set; }
    [DisplayName("Others")]
    public string Others { get; set; }
    [DisplayName("Why Exposed")]
    public string WhyExposed { get; set; }

    public virtual ICollection<ActScen> ActScens { get; set; }
}


public class ActScen
{
    [Key]
    [ForeignKey("ExpScen")]
    [DisplayName("Exposure Id")]
    public int? ExpId { get; set; }
    [DisplayName("Activity No")]
    public string ActNo { get; set; }
    [DisplayName("Method")]
    public string Method { get; set; }
    [DisplayName("Area")]
    public string Area { get; set; }
    [DisplayName("Exposure")]
    public string Exposure { get; set; }

    public ExpScen ExpScen { get; set; }

}


protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
        modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
        modelBuilder.Entity<ExpScen>().HasOptional(e => e.ActScens); 
}

一个 ExpScen 可以有 0 到多个 ActScen。如果我在 ExpScen 上做一个简单的查询: return _context.ExpScens.Where(r => r.ReqId == reqId).ToList();

ActScens 返回为 null,当我检查对象时可以看到这些错误:

{"Unable to cast object of type 'Infrastructure.Models.ActScen' to type   'System.Collections.Generic.ICollection`1[Infrastructure.Models.ActScen]'."}

{"Unable to set field/property ActScens on entity type System.Data.Entity.DynamicProxies.ExpScen_83F73B45A46F6AC263EB586EA84603C8228AF8B2673F23BF020CAC9C52BE4FE3. See InnerException for details."}

有人知道我在哪里出错了吗? 谢谢

【问题讨论】:

  • 我认为您必须将您的 ExpScen 属性设为虚拟。我不确定 Key 和 ForeignKey 字段“ExpId”。您是否尝试过为 ActScen 创建一个额外的 Key 字段?
  • 我认为你应该使用modelBuilder.Entity&lt;ExpScen&gt;().HasMany(e =&gt; e.ActScens).WithRequired(x =&gt; x.ExpScen);

标签: entity-framework ef-code-first foreign-key-relationship


【解决方案1】:

您的ActScen 模型存在许多问题。这些问题如下。

  1. ExpScen需要给virtual

  2. Primary key 不能有 null-able 数据类型

  3. 以下方式使用时无需提供[ForeignKey]属性

  4. 当使用 EF 约定时,将Id 作为主键如下所示。

    public class ActScen
    {
    [Key]
    [DisplayName("Exposure Id")]
    public int Id { get; set; }
    [DisplayName("Activity No")]
    public string ActNo { get; set; }
    [DisplayName("Method")]
    public string Method { get; set; }
    [DisplayName("Area")]
    public string Area { get; set; }
    [DisplayName("Exposure")]
    public string Exposure { get; set; }
    
    public virtual ExpScen ExpScen { get; set; }
    

    }

【讨论】:

    猜你喜欢
    • 2018-05-31
    • 2023-04-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多