user1085867
user1085867

Reputation: 71

Foreign key relationship in EF Code first not returning data

I'm hoping someone will be able to help me with a problem.

I'm using entity framework code first but i'm struggling with a foreign key relationship.

The relationship is set up like this:

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); 
}

An ExpScen can have 0 to many ActScen. If I do a simple query on ExpScen: return _context.ExpScens.Where(r => r.ReqId == reqId).ToList();

ActScens is returned as null and I can see these errors when I inspect the object:

{"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."}

Does anybody have any ideas where i'm going wrong with this one? Thanks

Upvotes: 1

Views: 1126

Answers (1)

Sampath
Sampath

Reputation: 65978

Your having number of Issues with your ActScen Model.Those are as below.

  1. Need to give virtual for ExpScen

  2. Cannot have null-able data type for Primary key

  3. No need to give [ForeignKey] attribute when you use below way

  4. When use EF conventions give Id as primary key is as shown below.

    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; }
    

    }

Upvotes: 1

Related Questions