Search code examples
c#sqlsql-serverasp.net-mvcentity-framework

Parameter is out of range on db.SaveChanges() in EF


I am developing a MVC app with Entity Framework. I am using Database first approach and in my Database there is a numeric field as:

[N15]  NUMERIC (1, 1)  NULL,

My code where I am getting error is:

public ActionResult allsave(VchrViewModel v)
{  
    List<TrD> chklist = this.dt;
    if (br.vchrbalance(chklist) == true)
    {
        trm = v.master;
        trm.S100 = vt;
        var n = db.TrMs.Max(x => x.N100);
        trm.N100 = n + 1;
        trm.S104 = getmonth(trm.D1.ToString());
        trm.S103 = getyear(trm.D1.ToString());
        db.TrMs.Add(trm);
        db.SaveChanges();
        seid = 0;
        var s = this.dt;
        foreach (TrD trd in s)
        {
            trd.S100 = vt;
            var d = db.TrDs.Max(x => x.N100);
            trd.N100 = d + 1;
            trd.N101 = v.master.N100;
            db.TrDs.Add(trd);

            db.SaveChanges();
            TrnYes4MST(trd.S1);
        }
        return Json(new { msg = "Y" });
    }
    else
    {
        return Json(new { msg = "N" });
    }
}

I am getting records for two different tables from the view. I have successfully saved the record in first table named "TrM" which has no value for N15. But in the foreach loop for table "TrD", for the first iteration N15 = 1. But on db.SaveCHanges() method I get the following error.

Parameter {1.0} is out of range

I have regenerated my edmx file but still unable to resolve.


Solution

  • The problem was with the EDMX. Only changes in database does not make it work. Since EDMX does not updates itself with the changes in database, so each time after committing a change in database, one has to update EDMX from Visual Studio.

    I have updated the design of my table in SQL Server and then updated the EDMX. After that I run the Custom Tool for my Model.tt file... And it Worked!!!