Entity Framework Decimal Inconsistency - entity-framework-4.1

I'm implementing the tutorial http://www.asp.net/mvc/tutorials/getting-started-with-aspnet-mvc3/cs/accessing-your-model's-data-from-a-controller
I use Entity Framework 4.1. In the database decimal value is mapped (18,2).
In the creating form I entered 1000 but in the details page the decimal value output is 1000,00 and also in the edit page as well.
I use #Html.EditorFor(model => model.Price) for input and for output.
When I looked at the database created by the Entity Framework the Price column which is my decimal value is created with this SQL command:
[Price] DECIMAL(18,2) NOT NULL
Why there is inconsistance?

You are comparing two different things. The mapping describes the precision which can be stored in the database but it has nothing to do with the way how your ASP.NET MVC View shows decimal number - it is handled by output formatting.

Related

hibernate converting char to binary

My JPA entity has a UUID attribute, which is stored as char(36) in mysql. When I query data i'm receving 66323735-3039-6262-2d31-3764392d3466 instead of f27509bb-17d9-4f37-b336-8603f2d34394. When I enabled hibernate logs, I could see
org.hibernate.type.descriptor.sql.BasicExtractor - extracted value ([col_1_0_] : [BINARY]) - [66323735-3039-6262-2d31-3764392d3466]
extracted value is [BINARY] instead of [VARCHAR] like other attributes.
Any clue as to why this is happening?
Also,
I've tried to run hibernate generated query on mysql and that returns correct results.
Other column values including UUIDs are being returned correctly.
I'm using an interface based entity projection to retrieve limited data and not the whole entity.
edit: I just added trim(colName) and now it's returning correct UUID value. But still not sure of the issue at hand.
Do this for hibernate 6. It will generate character type (for mysql char(36)).
#org.hibernate.annotations.JdbcTypeCode(SqlTypes.CHAR)
protected UUID id;

Pomelo.EntityFrameworkCore.MySql Database first and Generated column

in my table I have a Generated column of Decimal type,
COLUMN `quantidadeTotal` DECIMAL(18,4) GENERATED ALWAYS AS (((`quantidade` + `quantidadeReservada`) + `quantidadeBloqueada`)) VIRTUAL;
which is the sum of 3 other columns also of Decimal type, the problem occurs when I execute "scaffold", the generated model comes with this column in this format
public decimal? QuantidadeTotal { get; set; }
and in generated context also has reference to the
entity.Property(e => e.QuantidadeTotal)
.HasColumnName("quantidadeTotal")
.HasColumnType("decimal(18,4)");
column
if i try to insert some record in the table i get an error, even if not informing anything in the Generated column, i have to delete the reference of this column in the context and in the model to work.
is there any configuration for scaffold to ignore this type of column or any special treatment?
if not, every time I make a scaffold I will have to adjust all fields manually, and it would be a lot of work.
Pomelo.EntityFrameworkCore.MySql v3.2.4
thanks!
Thanks for reporting this bug on our repository as Database first and Generated column #1256!
We fixed it with:
3.2.x: Add scaffolding support for computed columns #1257
5.0.0+: Add scaffolding support for computed columns, including STORED support #1260

Entity Framework 4 - using DateTime2

I want Entity Framework to store all my dates as DateTime2 data type.
I have ProviderManifestToken="2008" in my SSDL and still all the generated dates are DateTime instead of DateTime2. What am I missing?
You are not missing anything. Entity framework never uses DataTime2 unless you manually modify its database generation process (only in model first approach). You need to update SSDLToSQL10.tt file to use DateTime2 instead of DateTime. Check the end of this answer for more details about modifying the template and configuring VS to use the new template.

Rails 3 and saving decimal values from a form

I have an odd bug in one of my applications.
When I am using the sqlite3 database the bug is not present. However when I use mysql2 as the database adapter I run into an error saving decimal values from a form.
If I submit the value 19.99 my input after the decimal is removed and it is stored in the database as 19.00
What would cause this? The database has the correct settings for the column and I can create a correct record using the rails console.
Edit: Said integer when I really wanted to say decimal.
I think it could be one of these possibilities:
A validation in your Rails model (or some step in your controller) is setting the value to an integer, or at least truncating the decimals, before saving to the database. To see if this is the case, check your INSERT operation in your logs and see what data Rails is trying to put in.
If Rails is sending 19.99 instead of 19.00, you most likely have a small discrepancy between data types in your two databases. Check to see if your MySQL database is simply not storing the decimal with a scale of 2 or higher. MySQL by default stores data types with a precision of 10 and a scale of 0, thereby NOT storing decimals.
If #2 is the problem, the solution is to generate a migration and 'change' your field type to specify a scale, something like:
# 'up' portion of a new migration
def self.up
change_column :mymodel, :myfield, :decimal, :precision => 6, :scale => 2
end

Get correct output from UTF-8 stored in VarChar using Entity Framework or Linq2SQL?

Borland StarTeam seems to store its data as UTF-8 encoded data in VarChar fields. I have an ASP.NET MVC site that returns some custom HTML reports using the StarTeam database, and I would like to find a better solution for getting the correct data, for a rewrite with MVC2.
I tried a few things with Encoding GetBytes and GetString, but I couldn't get it to work (we use mostly Delphi at work); then I figured out a T-SQL function to return a NVarChar from UTF-8 stored in a VarChar, and created new SQL views which return the data as NVarChar, but it's slow.
The actual problem appears like this: “description†instead of “description”, in both SSMS and in a webpage when using Linq2SQL
Is there a way to get the proper data out of these fields using Entity Framework or Linq2SQL?
Well, once you get the data out, you could always try this:
Encoding.UTF8.GetString(Encoding.Default.GetBytes(item.Description))
assuming the field is encoded in the system ANSI page. You might have to create the right encoding with Encoding.GetEncoding() if for some reason it isn't (looked up from DB type, for example).