Updating Entity Framework 6 Database C#

75 views Asked by At

I am trying to update my database and insert values into one of the columns "LongitudinalId", but as soon as I run the update-database command in my Package Manager Console, it gives me this error:

Cannot insert the value NULL into column 'LongitudinalId', table 'TruckDbWcf.dbo.Chassis'; column does not allow nulls. UPDATE fails. The statement has been terminated.

Here is the code where I am trying to insert the data:

            context.Chassis.AddOrUpdate<Chassis>(x => x.Name,
                new Chassis { Name = "DAIHATSU DELTA V116", LongitudinalId = context.StockItems.Where(x => x.StockCode == "SCH100").First().Id },
                new Chassis { Name = "DAIHATSU DELTA V116 PLUS", LongitudinalId = context.StockItems.Where(x => x.StockCode == "SCH100").First().Id },
                new Chassis { Name = "DAIHATSU DELTA V58", LongitudinalId = context.StockItems.Where(x => x.StockCode == "SCH100").First().Id },
                new Chassis { Name = "FAW", LongitudinalId = context.StockItems.Where(x => x.StockCode == "SCH100").First().Id },
                ... //It goes on for about 100 more lines
                new Chassis { Name = "VOLKSWAGEN VW 24-250 LWB (D AXLE)", LongitudinalId = context.StockItems.Where(x => x.StockCode == "SCH110").First().Id });

Now i've already checked that I am inserting data in every single line that requires the "LogitudinalId", but I can't figure out why it's giving me this much trouble and throwing the error that I gave above.

I have also tried to delete the database as well as to delete the current data that's already in that specific table, but I am getting no where. If anyone can help, I would be very greatfull! :)

EDIT- Class where I create the table with the LogitudinalId:

public class Chassis
{
    public int Id { get; set; }
    public string Name { get; set; }

    public int? LongitudinalId { get; set; }
    [ForeignKey("LongitudinalId")]
    public virtual StockItem ChassisLongitudinal { get; set; }
}
0

There are 0 answers