Mushfiq
Mushfiq

Reputation: 3

An error related to Entity Framework, .NET4, MVC3

I am new to this platform. So, I'm not familier with all the terminologies.
I'm getting the error message :

A specified Include path is not valid. The EntityType 'ERP.Models.OpeningBalance' does not declare a navigation property with the name 'AccountHeadId'.

Here I got a Model class 'OpeningBalance' in the project 'ERP'

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;

namespace ERP.Models.Account
{
    public class OpeningBalance
    {

        [Key]
        public int OpenningBalanceId { get; set; }

        [Required(ErrorMessage = "The {0} cannot be left blank.")]
        [Display(Name = "Openning Balance Date")]
        [DataType(DataType.Date)]
        [DisplayFormat(NullDisplayText = "", DataFormatString = "{0:dd-MM-yyyy}")]
        public DateTime OpenningBalanceDate { get; set; }

        [Required]
        [StringLength(100, ErrorMessage = "The {0} cannot be left blank.")]
        [Display(Name = "AccountHeadId")]
        public int AccountHeadId { get; set; }
        public virtual ChartOfAccount ChartOfAccount { get; set; }


    }
}

Isn't the public int AccountHeadId { get; set; } is

a navigation property with the name 'AccountHeadId'

?
The Source Error shows the lines from Controller :

    var openingBalances = db.OpeningBalances.Include(o => o.AccountHeadId);
    return View(openingBalances.ToList());  

I searched a lot in web including all related ques in stackoverflow, then tried the following form:

var openingBalances = from o in db.OpeningBalances.Include(o=>o.AccountHeadId) select o;  
return View(openingBalances.ToList());

That didn't work too. Anybody have any idea?

Upvotes: 0

Views: 2070

Answers (1)

Eranga
Eranga

Reputation: 32437

The AccountHeadId property is not a navigational property. A navigational property should be of type another entity such as ChartOfAccount or entity collection such as ICollection<ChartOfAccount>.

If the scalar property AccountHeadId is associated with the navigational property ChartOfAccount Then include ChartOfAccount in your query.

var openingBalances = db.OpeningBalances.Include(o => o.ChartOfAccount);

You use the Include method to eager load related entities to avoid multiple round trips to the database. Primitive properties such as AccountHeadId will be loaded always when you retrieve the entity that contain such properties.

Upvotes: 1

Related Questions