Entity Framework 两问1、在根据数据库更新 edmx 时,出现错误The model was generated with warnings or er
Entity Framework 两问 1、在根据数据库更新 edmx 时,出现错误
The model was generated with warnings or errors. Please see the Error List for more details. These issues must be fixed before running your application. Loading metadata from the database took 00:00:03.5636247. Generating the model took 00:00:15.7680284.
DataSourceDBEntities db1 = new DataSourceDBEntities(); var q1 = from f in db1.Foods select f; Food food = q1.FirstOrDefault<Food>(); db1.Detach(food); //移除状态管理
[其他解释] 对于第二个问题,如果我在系统中使用了“静态单例模式”,是不是不用考虑那么多了: public partial class DataSourceDBEntities { [ThreadStatic] private static DataSourceDBEntities instance; public static DataSourceDBEntities CurrentInstance { if(instance == null){ instance = new DataSourceDBEntities(); }
return instance; } }
使用时不再使用 new,而是调用静态方法得到当前线程的唯一实例: DataSourceDBEntities db = DataSourceDBEntities.CurrentInstance;
[其他解释]
但是在 EF 框架下,连接都是打开然后马上自动关闭的,并且访问数据库的前后都不需要主动打开和关闭操作,也没有将之放在 using(DataSourceDBEntities db = new DataSourceDBEntities ()){ } 而默认调用它的 Dispose 方法。 经过调试检查,发现 DataSourceDBEntities.Connection.State 值,访问数据库之前和之后,都是关闭状态。因此我判断 EF 会在每次访问数据库之前检查连接状态,并且在连接完成之后再关闭连接。