1

So my table on SQL Server has 2 tables

dbo.Account

AccountID   AccountStatusID ...
  1234             1   
  4321             2
  ....

dbo.validAccountStatus

AccountStatusID   AccountStatus
       1            Terminated
       2            Active
     ....

On Account Index page, I want to display the status instead of the status ID, something like this:

AccountID   AccountStatusID ...
  1234        Terminated 
  4321        Active
  ....

I keep getting this error:

System.InvalidOperationException: A specified Include path is not valid. The EntityType 'BAMSQLDBModel.Account' does not declare a navigation property with the name 'validAccountStatus'

I am doing database-first.

Models

Account.cs
public partial class Account
{
   public int AccountID { get; set; }
   [Display(Name = "Account Status ID")]
   public int AccountStatusID { get; set; }

   public virtual validAccountStatus validAccountStatus { get; set; }
}

validAccountStatus.cs
public partial class validAccountStatus
{
    [Key]
    public int AccountStatusID { get; set; }
    [Display(Name = "Account Status")]
    public string AccountStatus { get; set; }

    public virtual Account Account { get; set; }
}

Context

public partial class BAMSQLDB : DbContext
{
     public BAMSQLDB()
            : base("name=BAMSQLDB")
     {
     }

     public virtual DbSet<Account> Accounts { get; set; }
     public virtual DbSet<validAccountStatus> validAccountStatus { get; set; }


     protected override void OnModelCreating(DbModelBuilder modelBuilder)
     {           
        modelBuilder.Entity<Account>()
             .HasRequired(a => a.validAccountStatus).WithRequiredPrincipal(aS => aS.Account);
     }
}

Controller (I'm using PagedList.MVC for paging)

public ActionResult Index(int? page)
{
     var accounts = from acc in db.Accounts select acc;
     accounts = accounts.Include(accSt => accSt.validAccountStatus);

     accounts = accounts.OrderBy(acc => acc.AccountName);

     int pageSize = 20;
     int pageNumber = (page ?? 1);

     return View(accounts.ToPagedList(pageNumber, pageSize));
}
PTN
  • 1,658
  • 5
  • 24
  • 54

1 Answers1

1

try adding a DataAnnotation to your Account class

Account.cs

public partial class Account
{
   public int AccountID { get; set; }
   [Display(Name = "Account Status ID")]
   public int? AccountStatusID { get; set; }
   /* HERE */       
   [ForeignKey("AccountStatusID")] 
   public virtual validAccountStatus validAccountStatus { get; set; }
}

I thing that you'll need to change you Navigation of Account to a List, because each validAccountStatus can be linked in many accounts, correct?

validAccountStatus.cs

public partial class validAccountStatus
{
    [Key]
    public int AccountStatusID { get; set; }
    [Display(Name = "Account Status")]
    public string AccountStatus { get; set; }

    public virtual List<Account> Accounts { get; set; }
}

Changes in Controller

public ActionResult Index(int? page)
{
     IQueyable<Account> accounts = from acc in db.Accounts select acc;
     accounts = accounts.Include(accSt => accSt.validAccountStatus);

     accounts = accounts.OrderBy(acc => acc.AccountName);

     int pageSize = 20;
     int pageNumber = (page ?? 1);

     return View(accounts.ToPagedList(pageNumber, pageSize));
}
Alexis Diel
  • 1,246
  • 1
  • 9
  • 15
  • Still getting that error. I opened the database diagram and the navigation property field in Account and validAccountStatus is still blank. But thanks a lot for pointing out that Accounts should be a list in validAccountStatus though. – PTN Oct 09 '15 at 15:30
  • I've 2 tries: ONE. Change in IndexActtion `var account` to `IQueyable account`. TWO. create a Linq with joining table validAccountStatus. – Alexis Diel Oct 09 '15 at 15:42