0

I posted this on the entity framework developers site with no response so I'll ask here also.

By default EF Code First creates "not null" entries for integers. I'd like to create nullable integer entries. I've found C# examples (this site) but trying to do similar in VB.net doesn't work. EF seems to ignore "int?" and "nullable(of int)" property types. Has anyone figured out how to do this in VB? Also, just out of curiosity, why is the default "not null" for integers? Thanks in advance for any help.

user1626137
  • 233
  • 1
  • 3
  • 10
  • 1
    Can you post your model (relevant part) here? Integers cannot store null values and thus it is "not null" by default. You have to indeed use a nullable integer. – Styxxy Dec 19 '12 at 21:26
  • college professor will tell you there shouldn't be any nullable column in database design. – Evan Dec 19 '12 at 21:35
  • the primary key of the table/entity should not be null and other field is optional. – spajce Dec 19 '12 at 22:07
  • @Evan Really? I know this guy ( http://books.google.ch/books?id=406_pJtiJ6sC ) thinks so, but there is far from consensus and this does little to address the OP's question. – Matthew Dec 19 '12 at 22:14
  • @Matthew Don't be so serious. I'm not talking about any practical or real world implementation. But theretically speaking, null is not a value of any type in database, and, while I'm not an expert of relation database, I do remember nullable columns are not permitted when you do database normalization. – Evan Dec 19 '12 at 22:23
  • So is it generally accepted that integer database fields should be not nullable? I'm developing an app that requires a lot of numeric input and want to allow the client to do intermidiate saves to avoid accidental data loss. I know that I can force zeros or -1 in locations that haven't been entered but allowing nulls would eliminate this need. I like to follow preferred practices. Please advise. – user1626137 Dec 19 '12 at 23:18
  • No, the `primary key` is the only should be not nullable :) – spajce Dec 20 '12 at 00:02
  • What I have found is "Public Property foo() As System.Nullable(Of Integer)" does not create a nullable entry in the db. However, if using C# "public System.Nullable foo { get; set; } does create a nullable field. – – user1626137 Dec 20 '12 at 00:59
  • @user1626137 I don't know vb. but if you cannot make it work in you entity definition itself, you can try to use fluent api to set that integer property to be optional. – Evan Dec 20 '12 at 19:42

1 Answers1

0

int? is written in vb .net as the code below and should be enterpretated by EF the same.

Dim i As System.Nullable(Of Integer)
Mattias Josefsson
  • 1,147
  • 1
  • 7
  • 22
  • What I have found is "Public Property foo() As System.Nullable(Of Integer)" does not create a nullable entry in the db. However, if using C# "public System.Nullable foo { get; set; } does create a nullable field. – user1626137 Dec 19 '12 at 23:12