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

David91
  • 311
  • 2
  • 3
  • 8

1 Answers1

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

greenhoorn
  • 1,601
  • 2
  • 15
  • 39
  • 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 – David91 Jan 26 '15 at 13:22
  • How does the table in SQL look like? Is there an annotation "NOT NULL"? What happens if you just insert the line? – greenhoorn Jan 26 '15 at 13:30
  • This line in this moment is NOT NULL and i like to change to accept null values, – David91 Jan 26 '15 at 13:33
  • Maybe the answer of [this](http://stackoverflow.com/questions/15781968/ef-data-migrations-wont-detect-changes-when-adding-new-migration) will help you. – greenhoorn Jan 26 '15 at 13:36
  • 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 – David91 Jan 26 '15 at 14:36
  • Personally I would delete the table and create a new one.. [Look here](http://stackoverflow.com/questions/11011695/entity-framework-4-3-migrations-move-existing-data) – greenhoorn Jan 26 '15 at 14:45
  • But is a problem when you work's with multi-tenant and have more schemas createds,... I like a solution to this.. Thanks! – David91 Jan 26 '15 at 14:54
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/69608/discussion-between-greenhoorn-and-david91). – greenhoorn Jan 26 '15 at 14:56