none
Adding record to SQL Server in WPF RRS feed

  • Question

  • Hi,

    I've like some suggestions on the above.  I am creating my first WPF desktop application using MVVM pattern.  Now I am at the stage where I have a all the data necessary to add to SQL Server database.

    In my ViewModel I am passing to my Customer Repository class's AddNewCustomer method the Customer object/instance which I would like to add to db.

    I am using EntityFramework 5 in VS2012 and have Entity Model created with generated classes including Model.tt classes.  I am also using Linq to Entities to get some data from database. 

    My question now is what is the best approach to add new record to database?  Shall I create a Stored Procedure and call and pass all the column data or is there some better approaches?

    Thanks  

    Monday, July 8, 2013 11:09 AM

Answers

  • Provided that automatic change tracking is enabled and that you are using the same DbContext instance to fetch and update the entity objects you can simply call the DbContext.SaveChanges() method to persist the changes made on the client side.

    If you are using another context when saving the data back to the database, you can explicitly set the state of an the entity object then call SaveChanges():

    public void Update(Customer customer)
    {
        using (var context = new Entities())
        {
            context.Entry(customer).State = System.Data.EntityState.Modified;
            context.SaveChanges();
        }
    }
    
    One of the benefits using a ORM like Entity Framework is that you don't have to write any data access code.
    • Marked as answer by Alex Teslin Tuesday, July 9, 2013 9:05 PM
    Monday, July 8, 2013 1:44 PM

All replies

  • Provided that automatic change tracking is enabled and that you are using the same DbContext instance to fetch and update the entity objects you can simply call the DbContext.SaveChanges() method to persist the changes made on the client side.

    If you are using another context when saving the data back to the database, you can explicitly set the state of an the entity object then call SaveChanges():

    public void Update(Customer customer)
    {
        using (var context = new Entities())
        {
            context.Entry(customer).State = System.Data.EntityState.Modified;
            context.SaveChanges();
        }
    }
    
    One of the benefits using a ORM like Entity Framework is that you don't have to write any data access code.
    • Marked as answer by Alex Teslin Tuesday, July 9, 2013 9:05 PM
    Monday, July 8, 2013 1:44 PM
  • Thanks Magnum,

    I've tried, but received a run-time error "DbUpdateConcurrencyException was unhandled".  Would that be because I am having two instances of Customer and trying to save only one?

    I am browsing, but so far couldn't find the answer.

    Monday, July 8, 2013 3:18 PM
  • Sorry, misspelled your name there.
    Monday, July 8, 2013 3:18 PM
  • This exception is thrown when no rows in the database were affected by the call to SaveChanges. If you are using the Fixed concurrency mode, please refer to the following article for information about how to resolve optimistic concurrency exceptions: http://msdn.microsoft.com/en-us/data/jj592904.aspx

    If the concurrency mode is set to None (this is the default), make sure you are setting the value of the primary key property for all entities to be updated.

    Monday, July 8, 2013 3:37 PM
  • Thanks Magnus, it worked.  I think your code example was based on editing an existing object. I was trying to add a new record.  So just had to change one line there.  I love Entity Framework.  
    Tuesday, July 9, 2013 3:19 PM