1

I have class like this:

public class Bundle
{
    public int Id { get; set; }

    public DateTime CreateDate { get; set; }

    public Child Something { get; set; }
}

EF created a table like this:

| Id | CreateDate | Something_Id | 

What i'm trying now, is to add the Something_Id foreign key to the class. I don't want to load the whole 'Something'-object just to get it's Id. However EF demands a DB-update and the db update with the manually added integer property 'Something_Id' fails because Something_Id is not nullable. How can i add this foreign key to the class structure without loosing data or relations?

Leonardo Henriques
  • 736
  • 1
  • 7
  • 18
F.H.
  • 884
  • 1
  • 11
  • 26
  • did you configure the relationship Bundle->Child to use your exposed property Something_Id? Otherwise it will expect it to be another property and use another naming convention for the FK (else there would be no migration to apply, since the schema is identical) – DevilSuichiro Mar 06 '18 at 15:57
  • Yes i tried with naming convention (SomethingId) and with data-annotation -> [ForeignKey], it threw an exception 'Context has changed..' and on DB-update it said "Cannot insert the value NULL into column 'ComplexId', table 'VGLSY.dbo.ClientBids'; column does not allow nulls. UPDATE fails. The statement has been terminated." – F.H. Mar 06 '18 at 16:31
  • the column that raised the exception is on table ClientBids, not Bundle, also the column name is ComplexId, not Something_Id. I would assume you made differences to another table as well? – DevilSuichiro Mar 07 '18 at 07:41

1 Answers1

0

Ok stupid mistake on my side. The null-exception actually gave the hint. One entry in the database did not have a foreign-key set, therefore the class structure was not allowed to be non-nullable and the update failed.

F.H.
  • 884
  • 1
  • 11
  • 26