无法从数据库到列表获取产品

时间:2018-04-19 18:07:06

标签: c# mysql asp.net entity-framework

当我想使用从表中传输数据的方法到列表时,我有以下问题。 EntityFramework.dll中出现“System.Reflection.TargetInvocationException”类型的异常,但未在用户代码中处理

其他信息:调用目标引发了异常。 enter image description here

这是phpmyadmin中的架构我的数据库 enter image description here 这是我的edmx架构 enter image description here

Model.cs的部分代码

public partial class produkty
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public produkty()
{
    this.zamowienias = new HashSet<zamowienia>();
}

public int idproduktu { get; set; }
public int idtypu { get; set; }
public string nazwa { get; set; }
public byte cena { get; set; }
public string opis { get; set; }
public byte[] image { get; set; }

public virtual typproduktu typproduktu { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<zamowienia> zamowienias { get; set; }
}

public partial class typproduktu
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public typproduktu()
{
    this.produkties = new HashSet<produkty>();
}

public int idtypu { get; set; }
public string nazwa { get; set; }

[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<produkty> produkties { get; set; }

当我想要tu使用方法GetAllProducts时,这个ProductModel.cs的代码,当我想要运行时我有错误

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

/// <summary>
/// Summary description for ProductModel
/// </summary>
public class ProductModel
{
   public string InsertProduct(produkty product)
   {
      try
      {
        komisEntities db = new komisEntities();
        db.produkties.Add(product);
        db.SaveChanges();

        return product.nazwa + " Poprawnie wstawiono";
    }
    catch (Exception e)
    {
        return "Error:" + e;
    }
}

public string UpdateProduct(int idproduktu, produkty product)
{
    try
    {
        komisEntities db = new komisEntities();

        //Fetch object from db
        produkty p = db.produkties.Find(idproduktu);

        p.nazwa = product.nazwa;
        p.idtypu = product.idtypu;
        p.cena = product.cena;
        p.opis = product.opis;
        p.image = product.image;

        db.SaveChanges();
        return product.nazwa + " was succesfully updated";

    }
    catch (Exception e)
    {
        return "Error:" + e;
    }
}

public string DeleteProduct(int idproduktu)
{
    try
    {
        komisEntities db = new komisEntities();
        produkty produkt = db.produkties.Find(idproduktu);

        db.produkties.Attach(produkt);
        db.produkties.Remove(produkt);

        db.SaveChanges();

        return produkt.nazwa + "poprawnie usunięto";
    }
    catch (Exception e)
    {
        return "Error:" + e;
    }
}



public produkty GetProduct(int idproduktu)
{

        using (komisEntities db = new komisEntities())
        {
            produkty product = db.produkties.Find(idproduktu);
            return product;
        }

}





public List<produkty> GetAllProducts()
{
  //  try
 //   {
        using (komisEntities db = new komisEntities())
        {
          List<produkty> products = (from x in db.produkties
            select x).ToList();



            return products;

  }
  //  }
 //   catch (Exception ex)
  //  {
     //   return null;
  //  }

}




public List<produkty> GetProductsByType(int idtypu)
{       
     using (komisEntities db = new komisEntities())
     {
         List<produkty> products = (from x in db.produkties
                                      where x.idtypu == idtypu
                                      select x).ToList();
         return products;

    }

}

}

错误图像: enter image description here

我可以通过此功能将产品添加到列表以显示数据项和详细信息

       protected void Page_Load(object sender, EventArgs e)
{
    FillPage();

}





private void FillPage()
{
    ProductModel model = new ProductModel();
    List<produkty> products = model.GetAllProducts();

   if (products != null)
 {
  //     Response.Redirect("~/Pages/Login.aspx");
        foreach (produkty product in products)
        {
            Panel productPanel = new Panel();
            ImageButton imageButton = new ImageButton
            {
                ImageUrl = "~/Images/Products/" + product.image,
                CssClass = "productImage",
               PostBackUrl = string.Format("~/Pages/Product.aspx?id={0}", product.idproduktu)
            };
            Label lblName = new Label
            {
              Text = product.nazwa,
                CssClass = "productName"
            };
            Label lblPrice = new Label
            {
                Text = "£ " + product.cena,
                CssClass = "productPrice"
            };

            productPanel.Controls.Add(imageButton);
            productPanel.Controls.Add(new Literal { Text = "<br/>" });
            productPanel.Controls.Add(lblName);
            productPanel.Controls.Add(new Literal { Text = "<br/>" });
            productPanel.Controls.Add(lblPrice);

            //Add dynamic controls to static control
            pnlProducts.Controls.Add(productPanel);
        }
   }
    else
   //    Response.Redirect("~/Pages/About.aspx");
    pnlProducts.Controls.Add(new Literal { Text = "No products found!" });
}

}

但我不能,因为我有消息“找不到产品!”

1 个答案:

答案 0 :(得分:1)

我认为您遇到了数据输入问题:productky.cena在模型中的类型为byte,但在数据库中的类型为decimal(10,0)。我不认为这两者之间存在隐式转换,因此屏幕截图中的“指定的转换无效”错误。