Why does Entity Framework ColumnBuilder accept nullable bool to indicate nullable property?

Referring to a class System.Data.Entity.Migrations.Builders


in Entity Framework 6, take, for example, a method Int


If we create an EF migration, we add a column like this:

AddColumn("Table", "Column", c => c.Int());


In the last parameter, AddColumn

we go to c => c.Int()

where it c.Int()

calls a method Int()

in the ColumnBuilder class and returns ColumnModel


If we look at the definition of the method Int()

, we can see that the first parameter of this method is bool?

, which will determine whether the column in the database will be created as nullable or not.

The documentation states that paramater nullable

is "A value indicating whether the column allows null values." See https://msdn.microsoft.com/en-us/library/dn589880(v=vs.113).aspx . So my guess is that if it is true

, then the column will be assigned a NULL value if it is false

, and then non-NULL.

The question is, what happens if this is null

? Why should this attribute bool

be null?


source to share

1 answer

This will depend on the implementation MigrationSqlGenerator


For example, it System.Data.Entity.SqlServer.SqlServerMigrationSqlGenerator


to a statement ALTER TABLE ... ADD COLUMN ...

if nullable == false

, but otherwise adds nothing. For ALTER COLUMN

, nullable == false

adds NOT

before NULL

(which is in the generated expression, whatever).

So, nullable == null

will produce whatever the vendor implementation has by default.

https://entityframework.codeplex.com/SourceControl/latest#src/EntityFramework.SqlServer/SqlServerMigrationSqlGenerator.cs .



All Articles