1

I have created this table:

 public string Name { get; set; } public string Description { get; set; } [ForeignKey("CityId")] public City City { get; set; } public int CityId { get; set; } 

Now I like to set CityId to null, and I Try this:

 public string Name { get; set; } public string Description { get; set; } [ForeignKey("CityId")] public City City { get; set; } public int? CityId { get; set; } 

But when I create the new migration in Package Manager Console (add-migration Curso_CityIdNullable), the migration generated is empty...

 public override void Up() { } public override void Down() { } 

Any help?? Thanks

1 Answer 1

1

Your change doesn't set the CityId to null. It makes it nullable! That's not the same. What do you want, set a the field CityId in a row to null or set the field as nullable?

A list of data annotations for ef can you find here: http://www.entityframeworktutorial.net/code-first/dataannotation-in-code-first.aspx

Sign up to request clarification or add additional context in comments.

8 Comments

I like to create a line in this table and CityId can be null, I change the ? of int but when I create the migration is empty, as if i haven't created any change
How does the table in SQL look like? Is there an annotation "NOT NULL"? What happens if you just insert the line?
This line in this moment is NOT NULL and i like to change to accept null values,
Maybe the answer of this will help you.
Thanks greenhoorn, but this post don't helps me :( I only change the int to int? and type: add-migration migrationName, but the result is empty, no changes and the field remains NOT NULL insteadof NULLABLE
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.