views:

45

answers:

1

Hi,

I am using EF4 and MVC 2.

I am inserting a new record to the database, and I need it to return the new ID value.

My stored procedure ends like this:

SELECT SCOPE_IDENTITY() AS NewApplicationID;

Here is my action method:

public ActionResult CreateApplication(ApplicationViewModel applicationViewModel)
{
   if (ModelState.IsValid)
   {
      try
      {
         Mapper.CreateMap<ApplicationViewModel, Application>();
         var application = (Application)Mapper.Map(applicationViewModel, typeof(ApplicationViewModel), typeof(Application));

         var success = applicationRepository.InsertApplication(application);
      }
      catch (Exception ex)
      {
      }
   }

   return View("CreateApplication", applicationViewModel);
}

Here is my InsertApplication method in my repository class:

public int InsertApplication(Application application)
{
   db.Applications.AddObject(application);
   return db.SaveChanges();
}

I need to return the value of the new ID.

I hope someone can help.

Thanks.

A: 

I managed to get this one sorted out. When I added my stored procedure I mapped NewApplicationID to the ApplicationID property. So after the record is inserted it sets the ApplicationID property to the value returned by SCOPE_IDENTITY().

Brendan Vogt