问题:System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.InvalidOperationException: The class 'xxxx' has no parameterless constructor.
场景:查询角色信息
public Class Role:Entity<string> { public string Name{get;set;} public Role(string name){ this.Name = name; } }
查询:
var role = _roleRepository.GetAll().FirstOrDefault(s => s.Name == '');
解决:
The class 'xxxx' has no parameterless constructor这句话提示很明显,就是说没有无参构造,可惜心理上忽略和英文的薄弱,搞的蛋疼了大半天才发现
问题:EF "There is already an open DataReader associated with this Command which must be closed first."
1.出现问题
场景1:在插入数据之前,先进行判断是否存在
执行SqlDataReader.Read之后,如果还想用另一个SqlCommand执行Insert或者Update操作的话,
会得到一个错误提示:There is already an open DataReader associated with this Command which must be closed first.
2.解决方法
在ConnectionString中加上一个参数“MultipleActiveResultSets”, 将其值设置为true。
MultipleActiveResultSets可以使数据库连接复用。这样就不怕数据库的连接资源被耗尽了。使用方法很简单,只需要把它加到数据的连接字符串中即可。
例如:server=(local);Integrated Security = true;database=AdventureWorks;MultipleActiveResultSets=true;
问题:The object cannot be deleted because it was not found in the ObjectStateManager
1.出现问题
在EF事务中,进行删除时候出现这个问题
2.解决方法
数据上下文是不同的对象,导致了出错;在事务之外获取数据,然后在事务内操作导致的问题,把获取数据放在事务内获取就可以了