Home > Net >  eventlog shows invalid column name although I don't have that in my model
eventlog shows invalid column name although I don't have that in my model

Time:11-16

I have the following database model class called positions:

public class Positions
{
    public string? Id { get; set; }

    public string? ArticlePLU { get; set; }

    public string? ArticleName { get; set; }
    public string? ArticleGroupPlu { get; set; }
    public string? ArticleGroupName { get; set; }
    public string? MenuId { get; set; }
    public string? MenuName { get; set; }
    public string? MenuLineId { get; set; }
    public string? MenuLineName { get; set; }
    public decimal Quantity { get; set; }
    public decimal Price { get; set; } 

    public List<Vat>? Vat { get; set; }

    public List<AdditionalCost>? AdditionalCost { get; set; }

    public List<Components>? Components { get; set; }

    public Comment Comment { get; set; }

    [ForeignKey("Orders")]
    public string? OrdersId { get; set; }

}

I also have the following class called Comment:

    public class Comment
{
    public string? Id { get; set;}

    public string? Type { get; set;}

    public string? Value { get; set;}

    [ForeignKey("Components")]
    public string? ComponentsId;

    [ForeignKey("Positions")]
    public string? PositionsId;

}

The class comment is a child of positions but it can also be a child of components, which itself is a child of positions, but I don't think thats relevant to the problem right now.

The eventlog of IIS shows that whenever I try to send data with Postman it cannot recognize a column called "CommentId" which does not exist in any classes and isnt referenced anywhere. My guess is that EntityFramework, which im using, is doing something in the background because I have not configured everything correctly. The 'Comment' data is supposed to be in another table. When the data is received, the Comment should get an own unique Id through my controller class and EF handles the foreign key.

CodePudding user response:

Since the Comment is the non nullable Child of the Position, Entity Framework automatically add Foreign KeyCommentId column to the table Positions. In order to solve this, you can make the Comment to be Nullable by adding ? in the Comment Property in Position model

public Comment? Comment { get; set; }

This will remove EF Core from automatically add new column CommandId to table Positions

  • Related