Can a database column be configured to allow NULL even if the property type is not nullable?

ef-fluent-api entity-framework-core

Question

This question is related to EF Core Issue #13079: Columns for nested owned types don't allow NULL although owned by derived entity. But it may be of interest more generally (e.g. if an existing not-well-modeled database has to be matched). At least for nested owned types within derived entities with TPH it would provide a way to configure the database such that its schema by now is like it will be after the linked issue is fixed:
Can a database column for a property be configured to allow NULL even if the property type is not nullable? If yes, how?

Given the following type:

public class SubData
{
    public Int32 Prop { get; set; }
}

Is it possible to manually configure the database column for SubData.Prop to allow NULL?

IsRequired(false) cannot be used because Int32 is not nullable. If needed we may assume that the name of the column is known to be, say, Data_SubData_Prop, and that the name of the table is, say, Entity. Probably it could be done using SQL ALTER COLUMN. But can it be done by Fluent API, too?

1
1
8/27/2018 8:06:06 AM

Accepted Answer

Is it possible to manually configure the database column for SubData.Prop to allow NULL?

Yes, it is by using SQL, e.g. (for Microsoft SQL Server)

if ( context.Database.EnsureCreated() )
{
    context.Database.ExecuteSqlCommand("ALTER TABLE Entity ALTER COLUMN Data_SubData_Prop int NULL");
}

But can it be done by Fluent API, too?

Unfortunately, I don't know. (See my comment on the question regarding Microsoft.EntityFrameworkCore.Xyz.Internal namespaces.)

0
9/4/2018 11:44:40 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow