I have a Context with these two classes:
In the Brand class, there's a Bike objects list.
When I try to put the Bike list in an instance of Brand, the program returns me this exception:
Invalid column name 'Strokes'.
Invalid column name 'BrandBike_BrandID'.
This is my context:
public class MyContext: DbContext
{
public DbSet<Brand> Brands{ get; set; }
public DbSet<Bike> Bikes{ get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
Database.SetInitializer<MyContext>(null);
base.OnModelCreating(modelBuilder);
}
}
This is my class named Brand:
public class Brand
{
[Key]
public int BrandID { get; set; }
[Required]
public string Name { get; set; }
public Country Nationality { get; set; }
public virtual List<Bike> Models { get; set; }
}
public class Bike
{
[Key]
public int BikeID { get; set; }
public string Model { get; set; }
public BikeCategory Category { get; set; }
public int EngineCapacity { get; set; }
public int Strokes { get; set; }
public double Price { get; set; }
public virtual Brand BrandBike { get; set; }
}
This is the Brand table structure:
CREATE TABLE [dbo].[Brands] (
[BrandID] INT IDENTITY (1, 1) NOT NULL,
[Name] NVARCHAR (MAX) NULL,
[Nationality] INT NOT NULL,
CONSTRAINT [PK_dbo.Brands] PRIMARY KEY CLUSTERED ([BrandID] ASC)
);
And this the Bike table structure:
CREATE TABLE [dbo].[Bikes] (
[BikeID] INT IDENTITY (1, 1) NOT NULL,
[Model] NVARCHAR (MAX) NULL,
[Category] INT NOT NULL,
[EngineCapacity] INT NOT NULL,
[Price] FLOAT (53) NOT NULL,
[Brand_BrandID] INT NULL,
CONSTRAINT [PK_dbo.Bikes] PRIMARY KEY CLUSTERED ([BikeID] ASC),
CONSTRAINT [FK_dbo.Bikes_dbo.Brands_Brand_BrandID] FOREIGN KEY
([Brand_BrandID]) REFERENCES [dbo].[Brands] ([BrandID])
);
GO
CREATE NONCLUSTERED INDEX [IX_Brand_BrandID]
ON [dbo].[Bikes]([Brand_BrandID] ASC);
How can I overcome this issue?
Biketable structure.