开发者

Entity Framework 4.1 Code First: Adding and updating records through a service layer

开发者 https://www.devze.com 2023-03-11 19:30 出处:网络
I am using Entity Framework 4.1 Code First and ASP.NET MVC 3. I have a service class, and in this class I call my repository methods.

I am using Entity Framework 4.1 Code First and ASP.NET MVC 3.

I have a service class, and in this class I call my repository methods.

I got the adding of a new tutorial object to work, I just don't know how to update the record. Here is my insert and update methods for the service:

public class TutorialService : ITutorialService
{
   private ITutorialRepository tutorialRepository;

   public TutorialService(ITutorialRepository tutorialRepository)
   {
      this.tutorialRepository = tutorialRepository;
   }

   public void Insert(Tutorial tutorial)
   {
      tutorialRepository.Add(tutorial);
      tutorialRepository.Save();
   }

   public void Update(Tutorial tutorial)
   {开发者_运维技巧
      // Not sure what the code looks like here to call the repo methods
   }
}

My repository class:

public class TutorialRepository : ITutorialRepository
{
   PbeContext db = new PbeContext();

   public void Add(Tutorial tutorial)
   {
      db.Tutorials.Add(tutorial);
   }

   public void Save()
   {
      db.SaveChanges();
   }
}

Controller class:

public ActionResult Edit(EditTutorialViewModel editTutorialViewModel)
{
   // Other code

   if (!ModelState.IsValid)
   {
      return View("Edit", editTutorialViewModel);
   }

   // Mapping code here to Tutorial object
   Tutorial tutorial = (Tutorial)tutorialMapper.Map(editTutorialViewModel, typeof(EditTutorialViewModel), typeof(Tutorial));

   // Update the existing tutorial
   tutorialService.Update(tutorial);

   return RedirectToRoute(Url.TutorialList());
}

What would the update code look like? Is there a simpler way of doing this?


In your repository:

public void UpdateScalar(Tutorial tutorial)
{
    var original = db.Tutorials.Find(tutorial.ID);
    db.Entry(original).CurrentValues.SetValues(tutorial);
}

And then in your service:

public void Update(Tutorial tutorial)
{
    tutorialRepository.UpdateScalar(tutorial);
    tutorialRepository.Save();
}

Tigger's solution works too and has the benefit that it isn't necessary to load the original before updating. On the other hand setting the state to Modified forces that all properties are sent to the DB in an UPDATE statement, no matter if they changed or not. The solution above only sends the changed properties to the DB but requires to load the original before.

Both solutions only work with scalar properties. As soon as a more complex object graph and navigation properties are involved updating becomes more complicated.


You could use the same save method in the repository class for both the insert and update for example,

public void Save(Tutorial tutorial)   
{   
   db.Entry(tutorial).State = tutorial.Id == 0
                                     ? EntityState.Added
                                     : EntityState.Modified;

   db.SaveChanges();   
}
0

精彩评论

暂无评论...
验证码 换一张
取 消