2

我正在尝试使用通用存储库从我的数据库中获取一个对象。当我执行代码时,我收到错误消息:

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

1 回答 1

0

类型不匹配实际上可能在数据库中的其他字段(即:除了 ProjectID 之外)和您的 Project 类之间?确保 Project 类中的所有属性和数据类型与 Project 数据库表字段正确一致。

可能会出现意外的不匹配,例如:您的 Project 类中的布尔字段可能在数据库中被标记为 varchar 字段,反之亦然。

于 2013-06-07T00:33:47.407 回答