0

这是我找到的更新代码:

using (TestDBEntities ctx = new TestDBEntities())
{
    //Get the specific employee from Database

    Emp e = (from e1 in ctx.Emp
         where e1.Name == "Test Employee"
         select e1).First();

    //Change the Employee Name in memory
    e.Name = "Changed Name";

    //Save to database
    ctx.SaveChanges();
}

现在我正在做的是这样的:

 using(CRNNSTestEntities crnnsupContext = new CRNNSTestEntities())
 {
     CPersonalInfo t = ((IQueryable<CPersonalInfo>)Cache["personquery"]).First();

     t.Tombstone.Address = Address1.Text;
     System.Windows.Forms.MessageBox.Show(crnnsupContext.SaveChanges()+"");
 };

这是行不通的。所以我的问题是我必须写类似CPersonalInfo t = from t in ....

为什么我的方法不起作用?

谢谢

4

3 回答 3

0

您还可以将对象附加到之前的上下文中。

更多信息如何使用附加在这里

于 2012-04-04T13:43:21.907 回答
0

您需要CPersonalInfo t从您的实体crnnsupContext而不是您的实体Cache

于 2012-04-04T13:04:40.580 回答
0

你能改变这个吗

using (TestDBEntities ctx = new TestDBEntities())
{
    //Get the specific employee from Database

    Emp e = (from e1 in ctx.Emp
         where e1.Name == "Test Employee"
         select e1).First();

    //Change the Employee Name in memory
    e.Name = "Changed Name";

    //Save to database
    ctx.SaveChanges();
}

进入

using (TestDBEntities ctx = new TestDBEntities())
{
    //Get the specific employee from Database

    Emp e = (from e1 in ctx.Emp
         where e1.Name == "Test Employee"
         select e1).First();
    var entity = ctx.Emp.Find(e);
    //Change the Employee Name in memory
    entity.Name = "Changed Name";

    //Save to database
    ctx.SaveChanges();
}

于 2017-06-02T15:03:03.623 回答