私が取り組んでいるアプリケーションは、モデルのインスタンスの作成中にエラーを生成します。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 オブジェクトのリスト、作成、編集、および削除で機能することを望みますが、エンティティに提供された情報を使用して作成することさえできません。