0

I've created a sample project which you can download here.

Steps to recreate manually:

  1. Create new MVC3 web project.
  2. Add a .edmx
  3. Create an Entity named 'Account.'
  4. Create an Entity named 'AccountLineItem.'
  5. Add a Decimal field to 'AccountLineItem.'
  6. Create a one-to-many association between 'Account' and 'AccountLineItem', be sure to leave checked the option to create a navigation property on 'Account.'
  7. Add the code to update the DB anywhere, I used Index() on the HomeController:

    public ActionResult Index()
    {
        ViewBag.Message = "Welcome to ASP.NET MVC!";
    
        using (var db = new TestStoreContainer())
        {
            Account acc = new Account();
            acc.Name = "Test Account";
    
            AccountLineItem accItem = new AccountLineItem();
            accItem.Amount = 22.10M;
    
            acc.AccountLineItems.Add(accItem);
    
            db.Accounts.AddObject(acc);
            db.SaveChanges();
        }
    
        return View();
    }
    

As you can see, I am updating the DB with an Account, and an AccountLineItem with a Value of 22.10; However, after running this code, the value of 22 appears in SQLEXPRESS with no precision.

I discovered this behavior using a SQL 2008 R2 box, so I don't believe the issue is with Express.

Can anyone else replicate this behavior? Am I doing something blatantly wrong? All feedback is welcome.

abatishchev
  • 98,240
  • 88
  • 296
  • 433
Chase B. Gale
  • 376
  • 3
  • 11

1 Answers1

1

as you mentioned in your comment

'[Amount] decimal(18,0) NOT NULL'

this line tell us

  1. the column name is Amount
  2. DataType is decimal
  3. precision is 18 digits that can be stored, both to the left and to the right of the decimal point.
  4. scale is 0 digits that can be stored to the right of the decimal point.

so you must change the scale value to save the digits to the right of the decimal point. you cannot do that using EF you have to change it from sql server management studio.

Amir Ismail
  • 3,865
  • 3
  • 20
  • 33
  • This worked perfectly. I was thrown off because this only manifests itself when adding from a NavigationProperty. If I create a decimal field in the same way (18,0) on the main entity, it saves with precision. Confusing behavior to say the least. Thanks! – Chase B. Gale Aug 25 '11 at 15:13