0
我是初學者用MVC5編程。我正在使用MVC5和Entity框架6.我有一些相關的類,我使用第一種代碼方法。 之前澄清的關係我可以添加控制器沒有任何問題,但在那之後我有問題,例如看到這個圖片我不能在mvc dd控制器:序列不包含匹配的元素
這是我的代碼: location.cs
namespace NezamBarghLorestan.Models
{
[Table("TblLocation")]
public class Location
{
[Key]
public int ID { get; set; }
[Required]
[DisplayName("کشور")]
public string Country { get; set; }
[Required]
[DisplayName("استان")]
public string State { get; set; }
[Required]
[DisplayName("شهر")]
public string City { get; set; }
[DisplayName("منطقه")]
public string Zone { get; set; }
public virtual ICollection<Expert> experts{ get; set; }
}
}
and expert.cs
[Table("TblExpert")]
public class Expert:Person
{
[Required]
[DisplayName("کد عضویت")]
public string EnCode { get; set; } //شماره عضویت
[Required]
[DisplayName("شماره پروانه")]
public string LicenseNo { get; set; }//شماره پروانه
[Required]
[DisplayName("تاریخ اولین صدور پروانه")]
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{yyyy/MM/dd}", ApplyFormatInEditMode = true)]
[Column(TypeName = "datetime2")]
public DateTime? LicenseNoStart { get; set; }
[Required]
[DisplayName("تاریخ انقضای آخرین پروانه")]
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{yyyy/MM/dd}", ApplyFormatInEditMode = true)]
[Column(TypeName = "datetime2")]
public DateTime? LicenseNoEnd { get; set; }
[DisplayName("پایه طراحی")]
public Grade DesGrade { get; set; }
[Required]
[DisplayName("پایه نظارت")]
public Grade SupervisGrade { get; set; }
[DisplayName("امتیاز مثبت سالانه")]
public long Score { get; set; } //امتیاز مثبت سالانه
[DisplayName("امتیاز منفی سالانه")]
public int? NegScore { get; set; }//امتیاز منفی
[DataType(DataType.Password)]
[DisplayName("کلمه عبور")]
public string password { get; set; }
[DisplayName("شهر حوزه فعالیت ")]
public LorestanCity CityWork { get; set; }
[DisplayName("شهر محل صدور")]
public int? CityId { get; set; }
public virtual ICollection<ExpertsHistory> expertshistory { get; set; }
public virtual ICollection<Building> buildings { get; set; }
[ForeignKey("CityId")]
public virtual Location location { get; set; }
}
和數據訪問層
public class NezamBarghDAL:DbContext
{
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
modelBuilder.Entity<Expert>().HasOptional<Location>(s => s.location)
.WithMany(s => s.experts).HasForeignKey(s => s.CityId);
}
public DbSet<Person> people { get; set; }
public DbSet<Expert> Experts { get; set; }
public DbSet<Facility> Facilitys { get; set; }
public DbSet<Manager> Managers { get; set; }
public DbSet<Owner> Owners { get; set; }
public DbSet<Location> locations { get; set; }
public DbSet<BuildingGroup> buildingGroups { get; set; }
public DbSet<FieldOfStudy> fieldOfStudies { get; set; }
public DbSet<Building> buidings { get; set; }
public DbSet<ExpertsHistory> expertHistories { get; set; }
}
在此之後進行測試我刪除從階級關係,我添加CONTROLER沒有任何問題,並就回國了關係類,但跑項目,打開鏈接後,控制器我得到這個錯誤: 序列中沒有匹配的元素
Line 19: public ActionResult Index()
Line 20: {
Line 21: return View(db.locations.ToList());
Line 22: }
Line 23:
Source File: E:\WebSite\NezamBarghLorestan\NezamBarghLorestan\Controllers\LocationsController.cs Line: 21