Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I tell Entity Framework Function Import that a column returned by a stored procedure is not nullable?

I have an SQL Server stored procedure that ressembles this:

CREATE PROCEDURE [jp].[GetFoo]
    @Guid UNIQUEIDENTIFIER
AS

SELECT
    CONVERT(BIT, (CASE WHEN [dbo].[GetBar](T.Col2) = 3 THEN 1 ELSE 0 END)) IsGetBarCol2EqualToThree
FROM 
    [dbo].[MyTable] T
WHERE
    T.Col1 = @Guid

When I do Function Import / Get Column Information in EF, the inferred type of the column IsGetBarCol2EqualToThree is Nullable<bool>. But there is no way this field is going to be null, so I'd like it to be just bool. Is there a way to do this that would be persistent upon updating (ie that does not rely on modifying any generated code)?

The SQL Server version is 2005, I'm using Visual Studio 2010SP1 with EF 4, project is compiled against .net 4.0.

like image 588
Evren Kuzucuoglu Avatar asked Jan 19 '12 16:01

Evren Kuzucuoglu


2 Answers

Make this modification: isnull([dbo].[GetBar](T.Col2), 0)

like image 118
O.O Avatar answered Oct 16 '22 04:10

O.O


You can create complex type and then modify the Nullable property of the generated field. Can be useful if you don't want to change your sp.

step by step:

  • open your edmx
  • open model browser (View->Other Windows->Entity Data Model Browser)
  • navigate to the field in your generated complex type (*.emdx->Model->Complex Types->your type->field)
  • open properties window (press F4)
  • among properties there should be Nullable. You can change it here and it will not be overwritten on the next model update, but if you recreate the complex type you will loose your tweak.

Alternatively you can open edmx as xml and find the same property.

<ComplexType Name="...">
          <Property Type="Int32" Name="..." Nullable="true" />

ps: I tested it in VS2012, EF 5

like image 33
Stanislav Avatar answered Oct 16 '22 04:10

Stanislav