Zayar
Zayar

Reputation: 85

How do I Update Entity with DomainContext Submit Change?

In my project first i was trying to add data to First Table , after that add new data to Second Table , after Second Table data added i was trying to update First Table . I am beginner for Silverlight, I simply just don't know how to update using DomainContext, please see below code sample and guide me , thanks

 private PaymentDomainContext paymentContext;

    public PaymentTest()
    {
        InitializeComponent();
    }


    // Executes when the user navigates to this page.
    protected override void OnNavigatedTo(NavigationEventArgs e)
    {
    }

    private void buttonAdd_Click(object sender, RoutedEventArgs e)
    {
        paymentContext = new PaymentDomainContext();
        Customer cs = new Customer();
        cs.FirstName = "John";
        cs.LastName = "Smith";
        cs.InsertedDate = DateTime.Now;
        paymentContext.Customers.Add(cs);
        paymentContext.SubmitChanges();

    }

    private void buttonUpdate_Click(object sender, RoutedEventArgs e)
    {

        paymentContext = new PaymentDomainContext();
        Customer cs = new Customer();
        cs.UpdatedDate = DateTime.Now;
        paymentContext.Customers.Add(cs);
        paymentContext.SubmitChanges();
    } 

Upvotes: 0

Views: 1187

Answers (1)

Juan Carlos Velez
Juan Carlos Velez

Reputation: 2940

private PaymentDomainContext paymentContext;
private Customer cs=null;//Reference to the last created customer

public PaymentTest()
{
    InitializeComponent();
    paymentContext = new PaymentDomainContext();//new line
}


// Executes when the user navigates to this page.
protected override void OnNavigatedTo(NavigationEventArgs e)
{
}

private void buttonAdd_Click(object sender, RoutedEventArgs e)
{
    //paymentContext = new PaymentDomainContext();
    cs = new Customer();//Modified line
    cs.FirstName = "John";
    cs.LastName = "Smith";
    cs.InsertedDate = DateTime.Now;
    paymentContext.Customers.Add(cs);
    paymentContext.SubmitChanges();

}

private void buttonUpdate_Click(object sender, RoutedEventArgs e)
{

    //paymentContext = new PaymentDomainContext();
    //Customer cs = new Customer();
    if(cs!=null){
       cs.UpdatedDate = DateTime.Now;
       //paymentContext.Customers.Add(cs);
       paymentContext.SubmitChanges();
    }
} 

Upvotes: 1

Related Questions