我正在尝试使用通用存储库从我的数据库中获取对象。当我执行代码时,我收到错误消息:
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; }
}
答案 0 :(得分:0)
类型不匹配实际上可能在数据库中的其他字段(即:ProjectID除外)和Project类之间?确保Project类中的所有属性和数据类型都与Project数据库表字段正确重合。
可能存在意外不匹配,例如:Project类中的布尔字段可能会在数据库中标记为varchar字段,反之亦然。