2

データベース構造
次のような構造を持つ非常に非正規化された SQL テーブルがあります。

CREATE TABLE logistix.shipments
(
    shipment_id INT NOT NULL PRIMARY KEY,
    destination_id NVARCHAR(15) NOT NULL PRIMARY KEY,
    pallet_id INT NOT NULL PRIMARY KEY,
    destination_order INT NOT NULL,
    pallet_description NVARCHAR(40) NOT NULL
)

特定のレコードはそれぞれ固有のものですが、1 つの出荷で複数のパレットが複数の宛先に送られる場合があります。

.NET インターフェイス
これは、次のように構造化したい EF オブジェクトによって操作されます。

class ShippingContext : DbContext
{
        public virtual DbSet<Shipment> Shipments {get; set;}
}

class Shipment
{
    int ShipmentId {get; set;}
    List<Destination> ShipmentStops {get; set;}
}

class Destination
{
    string DestinationId {get; set;}
    int DestinationOrder {get; set;}
    List<Pallet> Pallets {get; set;}
}

class Pallet
{
    int PalletId {get; set;}
    string PalletDescription {get; set;}
}

問題
テーブルを 1 対 1 のエンティティに分割し、外部キー データを EF のコレクションにマッピングするチュートリアルは見つかりましたが、あるテーブルの列をコレクションにマッピングする方法については何も見つかりません。これは可能ですか、それともテーブルの分割、ビューの作成、または各列のプロパティを持つ POCO クラスの作成に限定されていますか?

Endmatter
別のアプリケーションが SQL テーブルにアクセスして、任意の数の出荷に関するレポートを生成するため、Powers That Be は、取得に時間がかかる一連の正規化されたテーブルとビューではなく、パフォーマンスのために非正規化されたテーブルを使用することを選択しました。 .

4

1 に答える 1

2

クラスはこれにリンクするように見えるはずです

public class ShipmnetContext : DbContext
{
    public DbSet<Shipment> Shipments { get; set; }
    public DbSet<Destination> Destinations { get; set; }
    public DbSet<Pallet> Pallets { get; set; }  
}

public class Shipment
{
    public int ShipmentId { get; set; }
    public ICollection<Destination> ShipmentStops { get; set; }

    public Shipment()
    {
        ShipmentStops = new HashSet<Destination>();
    }
}

public class Destination
{
    [Key]
    public string DestinationId { get; set; }
    public int DestinationOrder { get; set; }
    //[Required]
    public Shipment Shipment { get; set; } //Foreign key to Shipment table, make property NotNull by adding [Required] attribute
    public ICollection<Pallet> Pallets { get; set; }

    public Destination()
    {
        Pallets = new HashSet<Pallet>();
    }
}

public class Pallet
{
    public int PalletId { get; set; }
    public string PalletDescription { get; set; }
    public Destination Destination { get; set; } //Foreign key to Destination table
}
于 2016-09-15T19:19:08.260 に答える