LINK - EF6 Update/Insert/Delete model object from outside of DbContext
2015-11-24 11:16
471 查看
Update or Insert:
Delete by PK:
reference - http://stackoverflow.com/a/28050510
Partial Update:
public void ChangePassword(int userId, string password)
{
var user = new User() { Id = userId, Password = password };
using (var db = new MyEfContextName())
{
db.Users.Attach(user);
db.Entry(user).Property(x => x.Password).IsModified = true;
db.SaveChanges();
}
}
reference - http://stackoverflow.com/a/5567616
public void InsertOrUpdate(Blog blog) { using (var context = new BloggingContext()) { context.Entry(blog).State = blog.BlogId == 0 ? EntityState.Added : EntityState.Modified; context.SaveChanges(); } }reference - https://msdn.microsoft.com/en-us/data/jj592676.aspx
Delete by PK:
Customer customer = new Customer () { Id = id };
context.Customers.Attach(customer);
context.Customers.Remove(customer);
context.SaveChanges();
reference - http://stackoverflow.com/a/28050510
Partial Update:
public void ChangePassword(int userId, string password)
{
var user = new User() { Id = userId, Password = password };
using (var db = new MyEfContextName())
{
db.Users.Attach(user);
db.Entry(user).Property(x => x.Password).IsModified = true;
db.SaveChanges();
}
}
reference - http://stackoverflow.com/a/5567616
相关文章推荐
- websphere7下报错:javax.management.MalformedObjectNameException: Invalid character 的解决办法
- 刨根问底Objective-C Runtime(1)- Self & Super
- Jobject获取json集合
- typeof、instanceof、Object.prototype.toString.call() 区分对象类型
- MJExtension objectWithKeyValues json 转model
- 解读QT信号与槽机制里 QMetaObject::connectSlotsByName(QObject *o)的源码
- Objective C定义私有方法
- objective-c中Category 类别、扩展 专题总结
- javascript中的Function与Object,function与object
- Function 语意学(inside the c++ object model)
- objective-C 多态
- Swift和Objective-C混合编程
- Objective-C基础知识点总结
- SLua 中,从一个Lua脚本调用 另一个GameObject上的Lua脚本中的函数
- IOS开发基础Object-C(09)—OC内存管理(3)-@property的参数详解
- 解决Volley框架下JsonObjectRequest以POST提交数据,服务端无法获取数据的问题。
- Objective-C:神在细节之中
- Objective-C的hook方案(一): Method Swizzling
- Objective-C Runtime 运行时之四:Method Swizzling
- 《Objective-c开发范例代码大全》