指定的包含路径无效。 EntityType' '不声明名称为'的导航属性'

时间:2015-10-09 15:05:47

标签: c# entity-framework navigation-properties

所以我在SQL Server上的表有2个表

dbo.Account

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

dbo.validAccountStatus

AccountStatusID   AccountStatus
       1            Terminated
       2            Active
     ....

在帐户索引页面上,我想显示状态而不是状态ID,如下所示:

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

我一直收到这个错误:

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

我正在做数据库优先。

模型

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

上下文

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

控制器(我使用PagedList.MVC进行分页)

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

0 个答案:

没有答案