0

I try to use one-to-many relationship in Entity Framework 6 and I got an error Invalid column name 'MeetingStatusObj_Id, but if you see below I already using [ForeignKey("blabla")] to explicitly set the column name.

It looks like Entity Framework is ignoring the [ForeignKey("blabla")] for some reason.

BUT if I set the relationship using FluentAPI it will works perfectly. Am I missing something ?

here is my code.

public class MeetingDetail
{
    [Key]
    public Guid Id { get; set; }

    [Required]
    public int IdMeetingStatus { get; set; }
    [ForeignKey("IdMeetingStatus")]
    public MeetingStatus MeetingStatusObj { get; set; }
}


public class MeetingStatus
{
    [Key]
    public int Id { get; set; }

    public string Name { get; set; }

    public IList<MeetingDetail> MeetingDetailObj { get; set; }

}



//In class AppDBContext
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
   //Prevent error https://stackoverflow.com/a/6143116/7906006
   Database.SetInitializer<AppDbContext>(null);
   base.OnModelCreating(modelBuilder);

  //If i set the relationship using fluent API in here, it will work perfectly
}

EDIT :

Other annotations is not working too like [Column("YourColumnName")] , [NotMapped]

Wibisono Indrawan
  • 516
  • 1
  • 12
  • 25
  • EF doesn't know that `MeetingStatusObj` and `MeetingDetailObj` are two ends of one relationship because the names don't follow the default conventions. You need an `[InverseProperty]` attribute. – Gert Arnold May 12 '21 at 07:38
  • @GertArnold Thank you for the reply, Sorry for the late response., my office right now is overwhelmed by deadlines. For now, I'll use Fluent API for relationships. I will let you know when I already try you suggestion – Wibisono Indrawan May 16 '21 at 12:24

0 Answers0