zoukankan      html  css  js  c++  java
  • EF 常见异常总结

    问题: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.解决方法

      数据上下文是不同的对象,导致了出错;在事务之外获取数据,然后在事务内操作导致的问题,把获取数据放在事务内获取就可以了

  • 相关阅读:
    022、如何运行容器(2019-01-15 周二)
    ssh 跳板机部署
    021、镜像小结(2019-01-14 周一)
    020、搭建本地Registry(2019-01-11 周五)
    019、使用公共Registry (2019-01-10 周四)
    018、容器命名最佳实践 (2019-01-09 周三)
    017、RUN、CMD、ENTRYPOINT (2019-01-08 周二)
    016、Dockerfile 常用命令(2019-01-07 周一)
    015、调试Dockerfile(2019-01-04 周五)
    014、镜像的缓存特性(2019-01-03 周四)
  • 原文地址:https://www.cnblogs.com/xcsn/p/6307592.html
Copyright © 2011-2022 走看看