简体   繁体   中英

Entity framework. How to right map tables without primary key

I tried to solve this problem, but couldn't so I'm trying to do it another way.

I have a view that consist of 3 tables without any primary/foreign keys.

VS2015 generated this class:

[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; }
}

This entity represents sku-barcode table so I may have row's like these:

 Product  | Barcode
 -------- | --------
 product0 | barcode0
 product1 | barcode1 
 product2 | barcode2
 product2 | barcode2

Now, I need to group it somehow. I'm trying:

using (skumodel db = new skumodel())
{
    var query = db.SkuBarcodesViews.GroupBy(e => e.C_Ref)
        .Select(g => new { Barcode = g.C_Barcode });
}

But, then I see this error:

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?)

How can I solve this?

This is just a start; there are a lot of other tables in the database without keys/foreign keys that I want to work with through EF.

What is the right way to get data from tables without keys? How to map tables like these?

First of all, this is not a primary/foreign keys problem per se. LINQ grouping works not like usual SQL grouping, but more like a Dictionary . For your example, group with key product2 will have two values barcode2 for each occurence in the table. As we operate with objects in C#, each row is represented by SkuBarcodesView instance, so you need something like this if you want to get all the barcodes for the product:

using (skumodel db = new skumodel())
{
    var query = db.SkuBarcodesViews.GroupBy(e => e.C_Ref)
        .Select(g => new { 
                             Product = g.Key,
                             Barcodes = g.Select(x => x.C_Barcode) 
               });
}

Note, that for now there are no restrictions on the values in your table, so one product may have a lot of different barcodes or a lot of same barcodes etc. How would you tell which one is the right one? Of course, if you're sure there's only similar barcodes, you can do g.First().C_Barcode instead of the inner g.Select() in the above code to get single barcode.

Second, using GroupBy() here is an overkill, you can just use something like:

using (skumodel db = new skumodel())
{
    var query = db.SkuBarcodesViews
        .Select(x => new { Ref = x.C_Ref, Barcode = x.C_Barcode })
        .Distinct();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM