I tried to solve this problem, but couldn't所以我试图做另一种方式。实体框架。如何正确映射表没有主键
我有一个视图,包含3个没有任何主键/外键的表。
VS2015生成的这个类:
[Table("SkuBarcodesView")]
public partial class SkuBarcodesView
{
[Key]
[Column("_Code", Order = 0)]
[StringLength(11)]
public string C_Code { get; set; }
[Key]
[Column("_Description", Order = 1)]
[StringLength(150)]
public string C_Description { get; set; }
[Key]
[Column("_ProductCode", Order = 2)]
[StringLength(50)]
public string C_ProductCode { get; set; }
[Key]
[Column("_Ref", Order = 4)]
[StringLength(36)]
public string C_Ref { get; set; }
[Key]
[Column("_Barcode", Order = 5)]
public string C_Barcode { get; set; }
}
这个实体代表SKU条码表,所以我可以有行的像这样的:
Product | Barcode
-------- | --------
product0 | barcode0
product1 | barcode1
product2 | barcode2
product2 | barcode2
现在,我需要以某种方式组吧。我想:
using (skumodel db = new skumodel())
{
var query = db.SkuBarcodesViews.GroupBy(e => e.C_Ref)
.Select(g => new { Barcode = g.C_Barcode });
}
但是,后来我看到这个错误:
Severity Code Description Project File Line Suppression State
Error CS1061 'IGrouping' does not contain a definition for 'C_Barcode' and no extension method 'C_Barcode' accepting a first argument of type 'IGrouping' could be found (are you missing a using directive or an assembly reference?)
我该如何解决这个问题?
这只是一个开始;数据库中还有很多其他表没有我想通过EF使用的键/外键。
从没有密钥的表中获取数据的正确方法是什么?如何映射这些表?
如果你的观点不具有主键,你为什么用'[关键]'属性? –
该类是ado.net模型自动生成的。顺便说一句,我可以使用字段_ref像一个键,因为没有任何键属性我得到EntityType'SkuBarcodesView'没有定义键。定义此EntityType的关键字。 – zxyzxy
如果您使用现有数据库,则不是Code First。 – Gasper