Proxy 类
代理类是为了让EF实现lazy load才需要的
public class BloggingContext : DbContext
{
public BloggingContext()
{
this.Configuration.ProxyCreationEnabled = false;
}
}
这样就可以关掉了。
using (var context = new BloggingContext())
{
var blog = context.Blogs.Find(1);
var entityType = ObjectContext.GetObjectType(blog.GetType());
}
上面这个写法可以100%拿到Original Class, 如果 Proxy 是开启的状态,那么这将很好用
AsNoTracking
using (DB db = new DB()) { var product = db.products.AsNoTracking().FirstOrDefault(); var state = db.Entry(product).State; // Detached }
看到了吧,可以返回一个 Detached 的Entity, 可以帮助提升性能.
[NotMapped] prop不要map to sql
[DatabaseGenerated(DatabaseGeneratedOption.Computed)] //让sql来generate value
[ConcurrencyCheck] //并发控制
更新 2016-07-14
Entity framework sum
refer : https://ilmatte.wordpress.com/2012/12/20/queryable-sum-on-decimal-and-null-return-value-with-linq-to-entities/
double resultA = db.units.Where(u => u.type == "aa").Sum(u => (double?)u.amount) ?? 0; double resultB = db.units.Where(u => u.type == "aa").Sum(u => u.amount); //error
resultB 会有 error , 2个制作出来的 sql 语句是一样的, 问题出在 double 必须是nullable 丫