如何使用DomainContext更新实体提交更改?

时间:2011-07-24 03:47:05

标签: silverlight-4.0 ria

在我的项目中,我首先尝试将数据添加到First Table,之后将新数据添加到Second Table,在添加Second Table数据之后我试图更新First Table。我是Silverlight的初学者,我只是不知道如何使用DomainContext进行更新,请参阅下面的代码示例并指导我,谢谢

 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();
    } 

1 个答案:

答案 0 :(得分:1)

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();
    }
} 
相关问题