none
SQLCE 4.0 EF 4.3 Code First migrations Column((TypeName="money")] RRS feed

  • 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

All replies