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