我正在尝试使用通用存储库从我的数据库中获取一个对象。当我执行代码时,我收到错误消息:
String was not recognized as a valid Boolean.
我之前使用过 Single() 函数的存储库,这不会给出任何错误。
示例工作查询
User usr = Adapter.UserRepository.Single(u => u.userEmail.Equals("thomas.vanlauwe@gmail.com"));
失败的查询
public ActionResult Display()
{
Project project = new Project();
int id = 2;
project = Adapter.ProjectRepository.Single(p => p.ProjectID.Equals(id));
return View();
}
存储库
public T Single(Expression<Func<T, bool>> where)
{
try
{
IQueryable<T> query = IDbSet;
//query = PerformInclusions(includeProperties, query);
return query.Single(where);
}
catch (InvalidOperationException ex)
{
Console.WriteLine(ex);
return null;
}
}
错误弹出一行:return query.Single(where);
项目类
public class Project
{
public Project() {
Categories = new List<Category>();
}
public int ProjectID { get; set; }
[Display(Name = "Titel")]
public string ProjectTitle { get; set; }
[Display(Name = "Beschrijving")]
public string ProjectDescription { get; set; }
[Display(Name = "Deadline")]
public Nullable<DateTime> ProjectDeadlineDate { get; set; }
[Display(Name = "End register")]
public Nullable<DateTime> ProjectEndRegisterDate { get; set; }
[Display(Name = "Budget")]
public double ProjectBudget { get; set; }
public Boolean ProjectIsFixedPrice { get; set; } //Fixed price or budget / hour
public int ProjectUrgent { get; set; }
public Int16 ProjectDifficulty { get; set; }
public Nullable<DateTime> ProjectCreatedDate { get; set; }
public Boolean ProjectFinished { get; set; }
public Int16 ProjectRating { get; set; }
public string ProjectComment { get; set; }
public int CompanyID { get; set; }
public virtual ICollection<Category> Categories { get; set; }
}
我的数据库的 id 为 2,所以查询肯定会返回一个项目对象。我希望你有足够的信息,如有必要,我会更新我的代码。
提前致谢
编辑
我不知道这个错误的原因是什么,这就是我添加这段代码的原因,也许它与映射有关:
映射
/*************ProjectS**************/
modelBuilder.Entity<Project>().HasKey(t => t.ProjectID);
modelBuilder.Entity<Project>().HasMany(p => p.Categories)
.WithMany(cat => cat.Projects)
.Map(pc =>
{
pc.ToTable("category_has_project");
pc.MapLeftKey("project_id");
pc.MapRightKey("category_id");
}
);
班级类别
public class Category
{
public int CategoryID { get; set; }
public string CategoryName { get; set; }
public virtual ICollection<Project> Projects { get; set; }
}