0

I am using EF 5 in VS 2012. I am trying to save a new record to a table but is not getting saved in the table and also no error is thrown. Please help . I am attaching my code.

using (TestEntities context = new TestEntities(new TestEntityConnectionManager("Testconn").ConnectionString))
            {
                var newCan = new Can
                {
                    CanGUID = new Guid(canGuid),
                    CanName = canName,
                    CanDescription = canDescription,
                    DesignCandidateID = designCandidateId,
                    MigrationDataXml = migrationDataXml,
                    PersonID =  1,
                    LastModifiedPersonID = 1,
                    CreationDate = DateTime.Now,
                    LastModifiedDate = DateTime.Now,

                };
                context.Cans.AddObject(newCan);
                var paramOut =  context.SaveChanges();
                retval = paramOut.ToString();
Iman Mahmoudinasab
  • 6,861
  • 4
  • 44
  • 68
Navneet Duggal
  • 91
  • 1
  • 15
  • Can you show the whole method? I don't see anything glaring, other that is seems you are using the ObjectContext version and it is recommended to use DbContext. – Justin Aug 16 '13 at 21:41
  • Hi Justin, there is nothing else in this method, method has some parameters which I pass as value to new newCan class as shown above in my code snippet – Navneet Duggal Aug 17 '13 at 05:52
  • Put it into try catch and please tell us what the exception is – kkocabiyik Aug 17 '13 at 06:39
  • AddObject is not a method on `IDbSet` (ef 5), either you are posting the wrong code or you should be using `.Add` instead – undefined Aug 17 '13 at 11:48

2 Answers2

0

This doesn't look right,

var paramOut = context.SaveChanges();

Should be in your [HttpPost] method surrounded by if (ModelState.IsValid)

[HttpPost]
public ActionResult Create(CLASS Model)
{
        if (ModelState.IsValid)
        {
                db.Table.Add(Model);
                db.SaveChanges();
                return RedirectToAction("Index");
        }
        return View(Model);
}
0

If no errors are occured and no data is saved, the problem must be in your connection string.

In fact, it seems that your conStr doesn't connect to the database you want! And db initializer is created a new database (because of your custom conStr) and save the data in it.

Amin Saqi
  • 18,549
  • 7
  • 50
  • 70