user2607420
user2607420

Reputation: 21

decimal? in Entity Framework

I used the http://msdn.microsoft.com/en-us/data/jj200620 video to learn how to create a C# Console Application that accesses an old SQL Server database.

I have some of the fields in the database set as Money. The entity framework created a model from the existing database and made a property with the type decimal?. This makes it interesting to work with it in C# as a function becomes:

private static string WriteMoney(decimal? item, string output)

which works ok.

I can not find anything on the internet on decimal?. Clearly Money is a decimal, and I could fix the issue by changing the SQL database types. This is not causing a hassle, merely an interesting observation.

Anyone encountered this before?

Upvotes: 2

Views: 6191

Answers (2)

Matthew
Matthew

Reputation: 10444

SQL MONEY is a decimal sort of type with four decimal points.
It is functionally similar to SQL DECIMAL(19,4)

However, it is not recommended to use the MONEY type if you will be doing any non-scalar arithmatic, rather you should use the appropriate DECIMAL type:

Should you choose the MONEY or DECIMAL(x,y) datatypes in SQL Server?

This is an important differentiation from double or float which are base-2 units

As to the nullability, all SQL types can be nullable if they're set that way in the schema. If you don't want the EF to use a nullable type then you need to make the SQL field non-nullable and refresh your dbml.

Upvotes: 2

Tim
Tim

Reputation: 15247

decimal? is just syntactic sugar for Nullable<decimal>

You might want to read on Nullable if you don't know about it already.

C# doesn't have a "money" type like SQL does, so EF has to get the next closest thing, which is decimal. decimal is just a C# keyword for System.Decimal.

I'm guessing that your column that is of type money in SQL allows for nulls. That is why it is decimal? rather than just plain decimal, as System.Decimal is a struct and cannot be null.

Upvotes: 1

Related Questions