2013-04-07 42 views
0

我正在處理的應用程序在創建模型實例時會生成錯誤。我有產品和顏色(多對多)和ProductImage(許多ProductImage爲ProductColor)。多對多腳手架視圖生成錯誤_id1

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對象的工作,但因爲它甚至不可能創建一個提供給實體的信息。

回答

0

在實體圖中刪除並重新創建關聯後,我結束了ProductImage的ProductColorId而不是ProductColor_Id。

不太清楚我做了什麼不同的(也許檢查圖中的「添加外鍵屬性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,必須在實體公約指定的關係這樣

一個技巧:項目)的ProductImage現在看起來是這樣。日e圖:

在一對多的第一次單擊一側(在我的情況下一個ProductColor有很多ProductImage,所以我右鍵單擊ProductColor)。然後當選擇add new =>關聯時,它會自動將ProductColor(單擊的項目)設置爲1。現在在右側設置多個關係會自動檢查「添加外鍵」複選框。

如果我先點擊多邊(右鍵點擊ProductImage)。然後將左側下拉菜單更改爲多個右側選擇ProductColor,其中一個用於多重性,然後需要手動檢查「添加外鍵」複選框。