Entity Framework does not map columns that are defined as Decimal - c#

I have a hard time understanding why Entity Framework skips columns that are defined as decimal. I've tried deleting the model a couple of times and adding it back using the database first approach but for some reason a few columns are not mapped.
The ones that are defined as Date, Int or Text have absolutely no problem. The ones that are giving me a hard time are the decimal ones and I have defined them as such:
Name: Hours, Datatype: Decimal, Length/Set: 10,2, Unsigned: Checked, Allow Null: Unckecked, Zerofill: Unchecked, Default value: 0.00.
If I create a view with sums based on that same table, EF has no problem identifying the decimal columns. How can I add the missing columns to my model? What am I doing wrong and is there a workaround?
Thank you

After spending hours on this I finally figured it out. If you are facing the same problem make sure that your column are not UNSIGNED.
For some reason Entity Framework does not map decimal columns that are unsigned. Just uncheck that option and you should be good.

Related

Issue training model in ML.net. Issue changing model from TaxiFareExample. 'Features' must be a known-size vector of R4, but has type: Vec<I4, 2>

Issue training model in ML.net.
Issue changing model from TaxiFareExample. 'Features' must be a
known-size vector of R4, but has type: Vec.
I started with the TaxiFare Example and that works. But then I changed the model and added my own values and my data. I got the error about the Features above. I played with it for a while and tried limiting my data. Even tried predicting the Fare Amount again but using my values as the model (2 integers).
My data model is very basic, I won't to add more variables but was trying to start simple.
Some of the first lines (I am getting the first two values as int's and trying to predict the third as a float).
0,2,17.5
0,7,8
0,7,8.5
Asked a question here but haven't seen a response yet.
I was also getting the same exception and I found that :
You have to have a mandatory column named 'Label' in your input data class.
Something like this example.
When the model is trained and evaluated, by default, the values in the
Label column is considered as correct values to be predicted.
Or, You have to use the ColumnCopier like this, if you don't want to have the mandatory column named 'Label'.
And, preferably all the numeric values should be of type float (or string which has to be vectorized). Or According to this.
The names I4 and R4 means int and float for the purposes of data view type system.

Store values in separate, C# type-specific columns or all in one column?

I'm building a C# project configuration system that will store configuration values in a SQL Server db.
I was originally going to set the table up as such:
KeyId int
FieldName varchar
DataType varchar
StringValue varchar
IntValue int
DecimalValue decimal
...
Values would be stored and retrieved with the value in the DataType column determining which Value column to use, but I really don't like that design. So I thought I'd go this route:
KeyId int
FieldName varchar
DataType varchar
Value varbinary
Here the value in DataType would still determine the type of Value brought back, but it would all be in one column and I wouldn't have to write a ton of overloads to accommodate the different types like I would have with the previous solution. I would just pull the Value in as a byte array and use DataType to perform whatever conversion(s) necessary to get my Value.
Is the varbinary approach going to cause any performance issues or is it just bad practice to drop all these different types of data into a varbinary? I've been searching around for about an hour and I can't get to a definitive answer.
Also, if there is a more preferred method anyone can think of to reach the same conclusion, I'm all ears (or eyes).
You could serialize your settings as JSON and just store that as a string. Then you have all the settings within one row and your clients can deserialize as needed. This is also a safe way to add additional settings at any time without any modifications to your database.
We are using the second solution and it works well. Remember, that the disk access is in orders of magnitude greater, than the ex. casting operation (it's milliseconds vs. nanoseconds, see ref), so do not look for bottleneck here.
The solution can be to implement polymorphic association (1, 2). But I dont think there is a need for that, or that you should do this. The second solution is close to non-Sql db - you can dump as a value anything, might be as well entire html markup for a page. It should be the caller responsability to know what to do wit the data.
Also, see threads on how to store settings in DB: 1, 2 and 3 for critique.

Entity framework appending a decimal place to a field of type decimal(10,0)

I'm using EF database first. In my database I have a field which I know will always be 10 digits long, so naturally I've opted for a decimal(10,0) type, when I insert values into the table I can insert any number up to 10 digits long, however when I insert an entity with EF6 it adds a 0 decimal and then throws a parameter out of range value. The type of the field in my C# code is decimal
Here is the entity immediately before calling context.SaveChanges():
And a sanity check, here is the column in sql server:
EDIT:
Here is the EF mapping:
Just reported it on codeplex.
The error sounds like a bug in EF, but if it is always an integer, wouldn't it make more sense to use an int instead of decimal? I am thinking both for logical and performance sake.
This is actually a bug in SqlClient - SqlDecimal code doesn't handle the precision correctly for some edge cases.
I believe the culprit is here:
http://referencesource.microsoft.com/#System.Data/data/System/Data/SQLTypes/SQLDecimal.cs#416
However, given that this has been the behavior for a long time, it is very unlikely it will ever be fixed. I would recommend working around this by using bigint, like user1648371 and Andrew Morton have suggested.

How to keep previous and new value of data?

I’m currently working on a project where we need to archive and trace all the modified data’s.
When a modification surrender, we have to kept theses information
Who has modified the data?
When?
And … that’s why I’m asking this question: Keep the previous
and the new value of the data.
Quickly, I have to trace every modification for every data.
Example :
I have a name field why the value “Morgan”.
When I modify this value, I have to be able to say to the user that the 6th of January, by XXX, the value changed from “Morgan” to “Robert” …
I have to find a clean and generic method to do this because a large amount of data is concerned by this behavior.
My program is in C# (.NET 4) and we are using Sql Server 2008 R2 and NHibernate for the object mapping.
Do you any ideas, experience or solution about how to do a thing like that?
I am a little confused about at what point you want to have the old vs new data available. But, this can be done within a database trigger as in the following question:
trigger-insert-old-values-values-that-was-updated
NHibernate Envers its what you want :)
You must use NHibernate 3.2+ (3.2 is the current release).
Its easy like
enversConf.Audit<Person>();
You can get info here and here
I've been in the same situation as you. I ended up doing in this way:
Save an ActivityEntry in the database containing an identity column (if you have multiple objects that change), an action-indicator (could be "User changed firstname", as a int), date field, userId and most important a parameter field.
Combining the values from the parameter field and the action-indicator I'm able to make strings like "{0} changed {1}'s firstname from {2} to {3}" where my parameter values could be "John;Joe".
I know it feels kinda wrong saving these totally loosely typed values in the database, but I believe it's the only way around, without having a copy of each table.

Datatypes and screwy results PHP/C#/WinForms

I am having a lot of issues with the Decimal datatype in PHP and WinForms (C#). See, I have built a website and an Order and inventory management app for a client, but often the values will get all screwy. And I'm interested in how you handle pricing, discounts, freight. Do you use float, double decimal?
I swear, I had less troubles when I first started learning PHP and used VARCHAR for pricing! (And I'm not exagerating)
In my clients database, let's say I have the following:
Column Name Column Name Column Name Column Name
Price Quantity Discount LineTotal
ALl columns are of the decimal datatype, except for Quantity. Quantity column is int.
And when I'm doing very basic calculations, like:
$LineTotal = $Price * $Quantity;
I get stupid results, like this:
5.98888
7.
9.111
0.4442
And, worst of all, often it will just completely remove numbers from the calculation:
If for example the result of a multiplication is (or should be): 4.92
I'll go have a look in the database, and it'll say: 4
Where's my .92 cents?
What can be causing this? Am I doing something wrong?
AFAIK there is no decimal type support in PHP. So the question is probably about what type really is in $LineTotal, $Price and $Quantity variables. Consider investigation using: http://php.net/manual/en/function.gettype.php
Umm I'm not sure about PHP but when I had weird stuff happening as well when I used decimal types and C#. So I just use double now.

Categories

Resources