[英]Unable to retrieve a record from db using LINQ
I am trying to retrieve a record for an update using below 我正在尝试使用以下方法检索更新记录
var product= _db.Products.SingleOrDefault(x => x.Id == id);
if (product!= null)
{
product.StatusId = statusId;
_db.SaveChanges();
}
Product Model 产品型号
public int Id { get; set; }
public int TypeId { get; set; }
public int? CategoryId { get; set; }
public int? StatusId { get; set; }
public string Name { get; set; }
public ProductStatus Status { get; set; }
public ICollection<ProductStatus> ProductStatuses { get; set; }
But, while retrieving the record in the first line, it throws an exception saying {"Invalid column name 'ProductStatus_Id'."}
I do have a ProductStatus table but not sure if it has anything to do with it 但是,在检索第一行中的记录时,它引发了一个异常,提示
{"Invalid column name 'ProductStatus_Id'."}
我确实有一个ProductStatus表,但不确定是否与它有任何关系
Your model doesn't specify a foreign key to the ProductStatus
table, so it is trying to use EF's default convention, which is to look for a field called "ProductStatus_Id". 您的模型没有为
ProductStatus
表指定外键,因此它正在尝试使用EF的默认约定,即查找名为“ ProductStatus_Id”的字段。
You can fix it by either adding a column called ProductStatus_Id
to the Products
table (and make it a FK to ProductStatus
), or you can use a data annotation to specify a name (and also have a matching column). 您可以通过在
Products
表中添加一个称为ProductStatus_Id
的列(并将其设置为ProductStatus
的FK)来修复它,也可以使用数据注释来指定名称(并具有匹配的列)。
Or, if you don't need Product.ProductStatus
, remove the property from Product
(which you could do temporarily to test. 或者,如果不需要
Product.ProductStatus
,请从Product
删除该属性(可以暂时进行测试)。
Edit: This page (first google hit for "ef foreign key", no affiliation) has some examples http://www.entityframeworktutorial.net/code-first/foreignkey-dataannotations-attribute-in-code-first.aspx 编辑:此页面(第一个Google命中“ ef外键”,没有从属关系)有一些示例http://www.entityframeworktutorial.net/code-first/foreignkey-dataannotations-attribute-in-code-first.aspx
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.