0

私が取り組んでいるアプリケーションは、モデルのインスタンスの作成中にエラーを生成します。Product と Color (多対多) と ProductImage (多数の ProductImage から ProductColor) があります。

public partial class ProductColor
{
    public ProductColor()
    {
        this.ProductImages = new HashSet<ProductImage>();
    }
    public int Id { get; set; }
    [DefaultValue(0),DisplayName("Price Offset")]
    public Decimal PriceOffset { get; set; }
    public int ProductId { get; set; }
    public int ColorId { get; set; }

    public virtual Color Color { get; set; }
    public virtual Product Product { get; set; }
    public virtual ICollection<ProductImage> ProductImages { get; set; }
}

public partial class ProductImage
{
    public int Id { get; set; }
    [DisplayName("File name"),
      Required(),
      StringLength(255)]
    public string FileName { get; set; }
    public bool Default { get; set; }
    public int ProductColor_Id { get; set; }

    public virtual ProductColor ProductColor { get; set; }
}

public class testContext : DbContext
{
    public testContext() : base("name=testContext")
    {
    }
    public DbSet<Product> Products { get; set; }
    public DbSet<Manufacturer> Manufacturers { get; set; }
    public DbSet<ProductColor> ProductColors { get; set; }
    public DbSet<Color> Colors { get; set; }
    public DbSet<ProductImage> ProductImages { get; set; }
    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Product>()
            .HasMany(c => c.ProductColors);
    }    
}

ProductImage のコントローラーとビューをスキャフォールディングし、ProductImage のインデックスに移動した後、db コンテキストから ProductImages を取得しようとするとエラーが発生します。インスタンスを取得するために次の sql を使用する必要があるとエンティティが判断したのも不思議ではありません。

SELECT 
[Extent1].[Id] AS [Id], 
[Extent1].[FileName] AS [FileName], 
[Extent1].[Default] AS [Default], 
[Extent1].[ProductColor_Id] AS [ProductColor_Id], 
[Extent1].[ProductColor_Id1] AS [ProductColor_Id1]
FROM [dbo].[ProductImages] AS [Extent1]

ProductColor_Id1 はデータベースに存在しません。テーブルを作成したSQLは次のとおりです。

CREATE TABLE [dbo].[ProductColors] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [PriceOffset] decimal(7,2)  NOT NULL,
    [ProductId] int  NOT NULL,
    [ColorId] int  NOT NULL
);
CREATE TABLE [dbo].[ProductImages] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [FileName] nvarchar(255)  NOT NULL,
    [Default] bit  NOT NULL,
    [ProductColor_Id] int  NOT NULL
);
ALTER TABLE [dbo].[ProductColors]
ADD CONSTRAINT [PK_ProductColors]
    PRIMARY KEY CLUSTERED ([Id] ASC);
ALTER TABLE [dbo].[ProductColors]
ADD CONSTRAINT [FK_ProductColorColor]
    FOREIGN KEY ([ColorId])
    REFERENCES [dbo].[Colors]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;
CREATE INDEX [IX_FK_ProductColorColor]
ON [dbo].[ProductColors]
    ([ColorId]);
ALTER TABLE [dbo].[ProductColors]
ADD CONSTRAINT [FK_ProductColorProduct]
    FOREIGN KEY ([ProductId])
    REFERENCES [dbo].[Products]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;
CREATE INDEX [IX_FK_ProductColorProduct]
ON [dbo].[ProductColors]
    ([ProductId]);
ALTER TABLE [dbo].[ProductImages]
ADD CONSTRAINT [FK_ProductColorProductImage]
    FOREIGN KEY ([ProductColor_Id])
    REFERENCES [dbo].[ProductColors]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;
CREATE INDEX [IX_FK_ProductColorProductImage]
ON [dbo].[ProductImages]
    ([ProductColor_Id]);

データベースはエンティティ図から生成され、私には問題ないように見えます。ProductImage の作成時に select ステートメントに ProductColor_Id1 を追加した理由がわかりません。

十分な情報が提供され、これが簡単に解決できる一般的な間違いであることを願っています。これをお読みいただきありがとうございます。お役に立てれば幸いです。

スキャフォールディングされたコントローラーとビューが ProdcutImage オブジェクトのリスト、作成、編集、および削除で機能することを望みますが、エンティティに提供された情報を使用して作成することさえできません。

4

1 に答える 1

0

エンティティ図で関連付けを削除して再作成した後、ProductColor_Id ではなく ProductColorId を持つ ProductImage になりました。

私が別の方法で何をしたかはよくわかりません(おそらく、図の「ProductImageエンティティに外部キープロパティを追加する」をチェックしてください。

ダイアグラムからデータベースを再作成し、データベースからモデル クラスを再作成しました (別のプロジェクトで使用するため)。ProductImage は次のようになります。

public partial class ProductImage
{
    public int Id { get; set; }
    [DisplayName("File name"),
      Required(),
      StringLength(255)]
    public string FileName { get; set; }
    public bool Default { get; set; }
    public int ProductColorId { get; set; }

    public virtual ProductColor ProductColor { get; set; }
}

私が見る限り、唯一の違いは ProductColerId であり、この方法でリレーションを指定するためのエンティティの規則である必要があります。

ダイアグラムで作業するときのヒント:

1 対多の場合、まず片側をクリックします (私の場合、1 つの ProductColor には多くの ProductImage があるため、ProductColor を右クリックします)。次に add new => association を選択すると、自動的に ProductColor (クリックされた項目) が 1 に設定されます。右側に多数のリレーションを設定すると、「外部キーを追加」チェックボックスが自動的にチェックされます。

最初に多面をクリックする場合 (ProductImage を右クリック)。次に、左側のドロップダウンを多数に変更し、右側で複数の ProductColor を選択してから、[外部キーを追加] チェック ボックスを手動でオンにする必要があります。

于 2013-04-07T03:26:36.777 に答える