2

次のように構成された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を作成する方法を理解しようとしています。具体的には、関係のマッピング。ProductMapEntityTypeConfiguration<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

誰もが提供できるどんな助けも心からの感謝で満たされるでしょう。

4

2 に答える 2

3

Entity Framework Power Toolsを使い始めるまで、同じ問題がありました

これを使用すると、ビジネス オブジェクトやマッピング クラスなどの明確なエンティティを生成できます。素晴らしいデータ アクセス レイヤーを作成するのに役立つ良い記事:リバース エンジニア コード ファースト

そして、マッピングは次のようになるはずだと思います:

public class ProductMap : EntityTypeConfiguration<Product>
{
    public ProductMap ()
    {
        // Primary Key
        this.HasKey(t => t.ProductId);

        // Properties
        this.Property(t => t.Name)
            .IsRequired()
            .HasMaxLength(256);

        // Table & Column Mappings
        this.ToTable("Product");
        this.Property(t => t.ProductId).HasColumnName("ProductID");
        this.Property(t => t.Name).HasColumnName("Name");

        // Relationships
        this.HasMany(t => t.Products)
            .WithMany(t => t.Images)
            .Map(m =>
            {
                m.ToTable("ProductImages");
                m.MapLeftKey("ProductID");
                m.MapRightKey("ImageID");
            });
        }
    }
于 2012-11-29T22:09:28.587 に答える
0

私はそれを考え出した。データベース構造とエンティティ モデルとの間に論理的な切断がありました。

productImages テーブルは *-1 マッピングをサポートしているため、Products POCO クラスには ProductImages のコレクションが必要です。次に、エンティティを次のように構成する必要があります。

public class ProductImagesMap : EntityTypeConfiguration<ProductImage>
{
        //Other field and key configuration...

        this.HasRequired(t=>t.Image).WithRequiredDependent();

        this.HasRequired(t => t.Product)
        .WithMany(t => t.ProductImage)
        .HasForeignKey(d => d.SKU);
}

public class ProductImage
{
    public int ImageId { get; set; }
    public string SKU{ get; set; }
    public virtual Image Image { get; set; }
    public virtual Product Product { get; set; }
}

public class Product
{
    public Product()
    {
        this.Features = new List<Feature>();
    }

    public int ProductId { get; set; }
    public string Name { get; set; }
    public string SKU{ get; set; }
    public virtual Brand Brand { get; set; }
    public virtual ICollection<ProductImage> ProductImages { get; set; }
}

ProductMap クラスでこの関係を構成するのに苦労しました (親/母テーブルの Products として)。ProductImages クラスから構成するまで機能しなかったため、2 つの未解決の質問は次のとおりです。

1) どのエンティティが関係構成を駆動するかを決定するルールは何ですか?

2) ProductImage は製品と画像の間のリンク テーブルとしてのみ機能するため、Products POCO クラスを ICollection Images プロパティを持つように構成し、ProductImage エンティティをすべてバイパスする方法はありますか?

于 2012-11-30T16:26:50.230 に答える