Donate. I desperately need donations to survive due to my health

Get paid by answering surveys Click here

Click here to donate

Remote/Work from Home jobs

Why is my projects, while adding or retrieving data, look for a column name that has an underscore in it?

I have run into this issue before and solved the Issue by just changing the column name to Example: Parts_Material while the column name is Material and the table it is looking at is Parts. I am somewhat confused on why it looks for this when it is not the column name. Is there something that is in my code that may be wrong to cause it to look for a column name that doesn't exist? I also had a case before where I changed the column name and it still did not find it because now that it is changed my project looks for Parts_Material1. Just looking for a reason for this and I really do not know what it is called to go searching for it. thanks for your help.

Shopping cart class:

 public void AddToCart(Parts part)
    {
        // Get the matching cart and album instances
        var cartItem = storeDB.Carts.SingleOrDefault(
            c => c.CartId == ShoppingCartId
            && c.PartNumber == part.Material);

        if (cartItem == null)
        {
            // Create a new cart item if no cart item exists
            cartItem = new Cart
            {
                PartNumber = part.Material,
                CartId = ShoppingCartId,
                Count = 1,
                DateCreated = DateTime.Now
            };

            storeDB.Carts.Add(cartItem);
        }
        else
        {
            // If the item does exist in the cart, then add one to the quantity
            cartItem.Count++;
        }

        // Save changes
        storeDB.SaveChanges();
    }

Controller Action:

        public ActionResult AddToCart(string id)
    {

        // Retrieve the album from the database
        var addedPart = storeDB.Parts
            .Single(part => part.Material == id);

        // Add it to the shopping cart
        var cart = ShoppingCart.GetCart(this.HttpContext);

        cart.AddToCart(addedPart);

        // Go back to the main store page for more shopping
        return RedirectToAction("Index");
    }

Cart Model Class:

    public class Cart
{
    public int RecordId { get; set; }
    public string CartId { get; set; }
    public string PartNumber { get; set; }
    public int Count { get; set; }
    public System.DateTime DateCreated { get; set; }

    public virtual Parts Parts { get; set; }
}

Parts Model Class:

    public class Parts
{
    [Key]
    public string Material { get; set; }
    public string DrawingNumber { get; set; }       
    public string Description { get; set; }
    public string ExtDescription { get; set; }
    public string UnitOfMeasure { get; set; }
    public decimal SellingPrice { get; set; }
    public decimal StandardCost { get; set; }
    public decimal AverageCost { get; set; }
    public decimal LastCost { get; set; }
    public string PrimaryVendor { get; set; }
    public string VendorReference { get; set; }
    public string LeadTime { get; set; }
    public string PartImage { get; set; }
    public bool InStockQuanex { get; set; }
    public int UnitsInStock { get; set; }
    public bool Status { get; set; }

    public virtual List<OrderDetail> OrderDetails { get; set; }
}

Error on ShoppingCart Class:

Invalid column name 'Parts_Material'. 

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Data.SqlClient.SqlException: Invalid column     name 'Parts_Material'.

Source Error: 


Line 33:         {
Line 34:             // Get the matching cart and album instances
Line 35:             var cartItem = storeDB.Carts.SingleOrDefault(
Line 36:                 c => c.CartId == ShoppingCartId
Line 37:                 && c.PartNumber == part.Material);

[Edit] I have cleared this up. Apparently in Entity Framework, when you have tables that are joined by a column - Example: PartNumber --> Material - It looks for a FK that relates to the join as "Parts_Material" (Parts table Material column). So I renamed the column to be Material so it is joined as Material --> Material. And the issue goes away!

Comments