Search code examples
c#entity-frameworkentitynullable

data annotations entity framework


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


Solution

  • 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