0

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?

4
  • Show your table's structure Commented May 17, 2018 at 15:57
  • Add that to the question with proper formatting, so that everyone can read it more easily Commented May 17, 2018 at 16:06
  • Ok, I added it to the question Commented May 17, 2018 at 16:09
  • Please specify the Bike table structure. Commented May 17, 2018 at 16:15

1 Answer 1

1

To solve the "Invalid column name 'BrandBike_BrandID'" error, you need to add a column named BrandId to your table Bike. This Column should store the BrandId that the Bike is related to.

I do not have your Bike table structure, but the "Invalid column name 'Strokes'" indicates that there is no column named Strokes in that table.

Sign up to request clarification or add additional context in comments.

3 Comments

Ok, there are no the two columns, how do I add them?
As far as I can see, Bikes has a column named Brand_BrandID that refers to the related Brand. You need to add this property to you Bike model.
I used this topic to update the database. Thank you very much.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.