0

I am using EF to scaffold controllers and views for a student class I defined. The strange thing is, that when I use the scaffolded 'create' method to add a Student or try to load localhost/Student, I get an error notice:

SqlException: Invalid column name 'StudentId1'. Microsoft.Data.SqlClient.SqlCommand+<>c.b__164_0(Task result)

DbUpdateException: An error occurred while updating the entries. See the inner exception for details. Microsoft.EntityFrameworkCore.Update.ReaderModificationCommandBatch.ExecuteAsync(IRelationalConnection connection, CancellationToken cancellationToken)

In my student class, I do not define the column name as StudentId1, but as studentID. Does anyone know where this 1 gets concatenated?

This is my code for Student.cs

public class Student
{
    [Key]
    public Guid StudentId { get; set; }
    [Required]
    public int Studentnummer { get; set; }

    [Range(1, 100, ErrorMessage = "U bent te oud of nog niet oud genoeg om student te zijn. ")]
    public int Leeftijd { get; set; }
    public string Voornaam { get; set; }

    [MinLength(2)]
    public string Achternaam { get; set; }
    [DataType(DataType.EmailAddress, ErrorMessage = "Voer een geldig emailadres in.")]
    public string Email { get; set; }

    [DisplayName("Bevriende studenten")]
    public List<Student> Vrienden { get; set; }

    [DisplayName("Behaalde cijfers")]
    public List<Grade> Cijferlijst { get; set; }

    public Student() {
       
             }
    public Student(int nr) {
      
        Email = emailGen(nr);
        Studentnummer = nr;
    }

    public Student(int studentnummer, string voornaam)
    {
        this.Studentnummer = studentnummer;
        Voornaam = voornaam;
        Email = emailGen(studentnummer);
    }
    public Student(int studentnummer, string voornaam, string mailie)
    {
        
       this. Studentnummer = studentnummer;
        Voornaam = voornaam;
        Email = mailie;
    }
    internal string emailGen(int studentnummer)
    {
        string mail;
        mail = studentnummer + "@student.hhs.nl";
        return mail;
    }

}
Ajay2707
  • 5,690
  • 6
  • 40
  • 58
FArk
  • 41
  • 4
  • can you check your entity diagram via solution explorer (See link :https://stackoverflow.com/questions/18658078/how-do-you-create-a-visual-model-of-entityframework-code-first) , then select table and then f4 , check one by one column – Ajay2707 Nov 26 '20 at 10:44
  • 1
    Have you inspected your dbContext. This feels like when reverse engineering the database there may had been a naming conflict and that is were the '1' may came from. – Karen Payne Nov 26 '20 at 12:04
  • Have you tried `context.Database.Log = sqlTxt => System.Diagnostics.Debug.WriteLine(sqlTxt);` to display the generated sql in your debugger? – Harald Coppoolse Nov 26 '20 at 12:55

1 Answers1

1

I think your problem is the Relationship (Vrienden) - this must add an Id - so it automatically generates a Column StudentId - but because you already have one (Key) a 1 gets added.

see:

https://learn.microsoft.com/en-us/ef/ef6/modeling/code-first/data-annotations

Do:

[InverseProperty("VriendenId")]
[DisplayName("Bevriende studenten")]
public List<Student> Vrienden { get; set; }