Reputation: 14533
There might be some nuance to this question since previous questions describe a similar problem, but their suggestions don't seem to work in our project. The project uses Entity Framework 6.1 and a database first design pattern and the T4 code generation strategy. We have a number of views that generate calculated values that we know will always have a default value, but Entity Framework keeps changing the properties to Nullable
manually updating the properties works, but as soon as the EDMX is updated it reverts back to Nullable
.
Is there a way to force Entity Framework to not default to Nullable
properties? Ideally this is a pattern we can keep following since we use a number of views and constantly needing to update properties is quite tedious.
Upvotes: 2
Views: 565
Reputation: 14533
The answer is to ensure that the view reports as being not null
from SQL Server at which point Entity Framework will not default to Nullable
. This can be done by forcing the view column to be not null
. Briefly,
SELECT CASE Status WHEN 3 THEN 1 ELSE 0 END AS HasStatus
FROM dbo.Product
Will map the HasStatus
column to int, null
. However,
SELECT ISNULL(CAST(CASE Status WHEN 3 THEN 1 ELSE 0 END), 0) AS HasStatus
FROM dbo.Product
Will map the HasStatus
column to int, not null
.
Upvotes: 3