locked
ef-core savechanges - invalid columnname RRS feed

  • Question

  • User-309523270 posted

    Hi,

    I changed a database table (both updated and dropped and recreated the table).  Somehow, running the command in the package manager console:

    Update-Database -context <contextname>

    says that "No migrations were applied.  The database is already up to date."

    So, I changed the context manually. 

    When I execute context.savechanges(), it says "invalid column name 'column1'", but column1 does not exist in the current table.  It did exist in the previous version before I dropped and recreated the table.  'column1' is a foreign key - referencing another column.

    I have not been able to establish this FK relationship in the context

    entity.HasOne(d => d.TableNameForeignKeyReferenceTable)
    .WithMany(p => p.TableName)
    .HasForeignKey(d => d.foreignKeyColumn)
    .OnDelete(DeleteBehavior.ClientSetNull)
    .HasConstraintName("foreignkeyname");

    thus,

    entity.HasOne(d => d.PersonAliasPerson) /* compiler is not recognizing this */
    .WithMany(p => p.PersonAlias)
    .HasForeignKey(d => d.PersonId)
    .OnDelete(DeleteBehavior.ClientSetNull)
    .HasConstraintName("FK_PersonAlias_Person");

    Table Structure
    =============
    Person
    =============
    PersonId
    DL
    SSN

    PersonAlias
    =============
    PersonAliasId
    PersonId
    LastName
    FirstName

    Thank you

    tinac99

    Friday, August 24, 2018 9:28 PM

All replies

  • User1120430333 posted

    So what can't you delete the EF model out of the project and regenerate it fresh based on the existing DB schema? That's what I would do if it were me. But I don't use EF code first. 

    Saturday, August 25, 2018 11:50 AM
  • User475983607 posted

    The first step is learning "Code First".  Don't delete a table because then your migrations snapshot files become out of sync with the database.  In Code first you back out database updates then reapply if necessary.  Like undo.

    If you want to make changes directly in the DB then you'll need to re-scaffold the entities after every change.

    I recommend that you go through the following site to learn EF Core.

    https://www.learnentityframeworkcore.com/

    Saturday, August 25, 2018 12:24 PM