我正在处理的应用程序在创建模型实例时生成错误。我有产品和颜色(多对多)和 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 的索引后,尝试从数据库上下文中获取 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 对象时工作,但因为它甚至不可能使用提供给实体的信息来创建一个。