简体   繁体   中英

Entity Framework Code First Optional Property is mapped as Required

I have an issue with Code First. I just migrate the project from Model First to Code First and I think that Entity Framework is losing its mind... Here is my class (simplified) :

public class MyClass
{
    // Key, other properties...

    public bool? MyNullBoolean { get; set; }

    public static void Configure(EntityTypeConfiguration<MyClass> myClass)
    {
        // Other configuration on other properties...
        myClass.Property(m => m.MyNullBoolean).IsOptional();
    }
}

Here are some additionnal informations for you :

  • I call the Configure() method in the OnModelCreating() method.
  • Migrations are enabled and Automatic mode is set to false
  • In my migration I have MyNullBoolean = c.Boolean(nullable: false) , so EF is acting like this property is required, wich is not as previously seen.
  • I target an existing DB (dev) with data inside so I can't perform Migration "for testing purposes"
  • This is the exact same model as the one from Model First and that last one is working correctly...

Here is what I tried :

  • Disabling / Enabling EF on the project (deleting Migrations in project and db)
  • Restarting VS
  • Targeting another DB (same structure)
  • Leave the property unconfigured (so EF do the job alone)

I'm out of idea and I found nothing in the Internet. Thanks for your help guys !

I think that EF was corrupted because when I uninstall it, clean the solution, install it, rebuild the solution, it worked.

So there was no problem with db or my model.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM