簡體   English   中英

查詢給出錯誤:字符串未被識別為有效的布爾值

[英]Query gives error: String was not recognized as a valid Boolean

我正在嘗試從具有通用存儲庫的數據庫中獲取對象。 當我執行代碼時,我收到錯誤消息:

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

類型不匹配可能實際上是在數據庫中的其他字段(即ProjectID以外的其他字段)與您的Project類之間? 確保Project類中的所有屬性和數據類型與Project數據庫表字段正確匹配。

可能存在意外的不匹配,例如:Project類中的布爾字段可能在數據庫中標記為varchar字段,反之亦然。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM