Answered by:
SQLCE 4.0 EF 4.3 Code First migrations Column((TypeName="money")]

Question
-
I'm playing around with codebased migrations using SqlCE 4.0 and EF 4.3. Have the following simple model class:
public class JournalEntry { public int Id { get; set; } public decimal Amount { get; set; } }
Running "Add-Migration Initial" generates the following code for the class:
CreateTable( "JournalEntries", c => new { Id = c.Int(nullable: false, identity: true), Amount = c.Decimal(nullable: false, precision: 18, scale: 2), }) .PrimaryKey(t => t.Id);
Running "Update-Database -Script" generates the following sql:
CREATE TABLE [JournalEntries] ( [Id] [int] NOT NULL IDENTITY, [Amount] [decimal](18, 2) NOT NULL, CONSTRAINT [PK_JournalEntries] PRIMARY KEY ([Id]) )
So far everything is ok. I can store "1.99" in Amount and call SaveChanges. Querying for it later on give me back just what I stored. No loss of precission.
Now I want to change the storetype for the Amount property to "money" by using ColumnAttribute. The class is changed to:
public class JournalEntry { public int Id { get; set; } [Column(TypeName="money")] public decimal Amount { get; set; } }
Running "Add-Migration Initial -Force" generates code:
CreateTable( "JournalEntries", c => new { Id = c.Int(nullable: false, identity: true), Amount = c.Decimal(nullable: false), }) .PrimaryKey(t => t.Id);
and running "Update-Database -Script generates sql:
CREATE TABLE [JournalEntries] ( [Id] [int] NOT NULL IDENTITY, [Amount] [decimal](18, 0) NOT NULL, CONSTRAINT [PK_JournalEntries] PRIMARY KEY ([Id]) )
Looks like EF 4.3 failed to generate the correct storetype, and more worrying, the precission/scale is lost!!! It is no longer possible to store and retrieve "1.99" without loosing precission.
How to fix?
...without having to manually correct the generated code each time i run "Add-Migration Initial -Force" with:
Amount = c.Decimal(nullable: false, storeType: "money")
- Moved by ErikEJMVP Sunday, February 12, 2012 9:20 AM (From:SQL Server Compact)
Saturday, February 11, 2012 9:54 PM
Answers
-
Hi ErikEJ,
Welcome!
I can repro your scenario, I think you should edit the DbMigration as your ways. I'll report this issue to Product team, Thanks for understanding.
Have a nice day.
Alan Chen[MSFT]
MSDN Community Support | Feedback to us
Get or Request Code Sample from Microsoft
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
- Marked as answer by Alan_chen Thursday, February 16, 2012 5:36 AM
Monday, February 13, 2012 8:55 AM -
Hi ErikEJ,
Yes, It is a know issue in EF4.3 and it will be fixed in EF4.3.1, thanks for understanding.
Have a nice day.
Alan Chen[MSFT]
MSDN Community Support | Feedback to us
Get or Request Code Sample from Microsoft
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
- Marked as answer by Alan_chen Thursday, February 16, 2012 5:36 AM
Thursday, February 16, 2012 5:36 AM
All replies
-
Hi ErikEJ,
Welcome!
I can repro your scenario, I think you should edit the DbMigration as your ways. I'll report this issue to Product team, Thanks for understanding.
Have a nice day.
Alan Chen[MSFT]
MSDN Community Support | Feedback to us
Get or Request Code Sample from Microsoft
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
- Marked as answer by Alan_chen Thursday, February 16, 2012 5:36 AM
Monday, February 13, 2012 8:55 AM -
Hi ErikEJ,
Yes, It is a know issue in EF4.3 and it will be fixed in EF4.3.1, thanks for understanding.
Have a nice day.
Alan Chen[MSFT]
MSDN Community Support | Feedback to us
Get or Request Code Sample from Microsoft
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
- Marked as answer by Alan_chen Thursday, February 16, 2012 5:36 AM
Thursday, February 16, 2012 5:36 AM