EntityFramework列名无效 [英] EntityFramework Invalid Column Name

查看:189
本文介绍了EntityFramework列名无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

更新:将BasketID的数据类型与int匹配不能解决问题。

UPDATE: Matching both datatypes of BasketID to int does not resolve the issue.

导致错误的代码:

 BasketItem duplicateBasketItem = (from thisitem in storeDB.BasketItems
                                   where thisitem.sellerSKU == newItem.sellerSKU
                                   select thisitem).SingleOrDefault();

正在抛出的错误:

System.Data.Entity.dll中发生了类型为System.Data.EntityCommandExecutionException的异常,但未在用户代码中处理

{无效的列名称BasketID。}

我的模型: p>

My Models:

public class Basket
{
    [Key]
    public int  BasketID { get; set; }
    public List<BasketItem> BasketItems { get; set; }
    public int Count { get; set; }
    public System.DateTime DateCreated { get; set; }
    public Guid UserID { get; set; }
}


public class BasketItem
{
    public int BasketItemID { get; set; }
    public virtual string BasketID { get; set; }
    public int sellerID { get; set; }
    public string sellerSKU { get; set; }
    public int Quantity { get; set; }
    public decimal Price { get; set; }
}

public class StoreEntities : DbContext
{
    public DbSet<Order> Order { get; set; }
    public DbSet<OrderItems> OrderItems {get; set;}
    public DbSet<Basket> Basket { get; set; }
    public DbSet<BasketItem> BasketItems { get; set; }
}


推荐答案

没有直接访问数据库环境的轻微的解决方法

Was able to fix with a slight workaround by not accesing the db context directly

ShoppingBasket myBasket = new ShoppingBasket();
        myBasket.ShoppingBasketId = basketID;

        Basket storebasket = (from ph in storeDB.Basket
                                      where ph.BasketID == myBasket.ShoppingBasketId
                                      select ph).SingleOrDefault();

这篇关于EntityFramework列名无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆