次のように構成された3つのテーブルを持つデータベースがあります。
Table 1: Product
Columns: ProductID, PK
SKU, PK
Name
Table 2: ProductImages
Columns: ImageID PK
SKU
Table 3: Images
Columns: ImageID PK
ImageContent
ProductImages
一意のPK制約が強制的に1対1になり、したがって不要な1対多対1のテーブル(既存のテーブル)であることを除いて、テーブルが多対多の関係にあるように見えることをしばらく無視します。データベース)。
次のPOCO
エンティティクラスが必要です。
public class Product
{
public int ProductId { get; set; }
public string SKU { get; set; }
public virtual Image Image { get; set; }
}
Image
私のエンティティモデルのエンティティでもあると仮定します。
私はEntity Framework 5.0
最初にコードを使用していて、クラス(から派生)Fluent API
を作成する方法を理解しようとしています。具体的には、関係のマッピング。ProductMap
EntityTypeConfiguration<Product>
レンダリングされたSQLは、EntityFrameworkの次のバージョンのようになります。
select p.SKU, p.Name, p.ProductID, I.ImageID, I.ImageContent
from Products p
inner join ProductImages si on p.SKU = si.SKU
inner join Images i on i.ImageId = si.ImageId
誰もが提供できるどんな助けも心からの感謝で満たされるでしょう。