您应该使用InsertOnSubmit方法:
NorthwindDataContext context = new NorthwindDataContext();
context.Products.InsertOnSubmit(new Product(..));
context.SubmitChanges();
Add方法在EntitySet的成员存在,增加儿童实体父之一,当大多使用,例如:
var category = new Category{ Name = "Breveages"};
category.Products.Add(new Product{ Name = "Orange Juice"});
category.Products.Add(new Product{ Name = "Tomato Juice"});
category.Products.Add(new Product{ Name = "Cola"});
//...
context.Categories.InsertOnSubmit(category);
// This will insert the Category and
// the three Products we associated to.
编辑:要执行更新操作,您只需通过查询或附加它来检索实体,例如:
var customer = context.Customers.Single(c => c.CustomerID == "ALFKI");
customer.ContactName = "New Contact Name";
context.SubmitChanges();
DataContext的跟踪其相关实体的变化和当的SubmitChanges方法被调用时,它会检测到变化,并生成场景做更新操作后面的更新SQL语句...
来源
2009-06-29 01:41:23
CMS
谢谢 。 。这工作,但现在我意识到,我不想添加一个新的,而是更新现有的。 。更新现有记录的方法是什么。 – leora 2009-06-29 01:47:27