• EF 使用遇到过的错误记录备忘


    1. is only supported for sorted input in LINQ to Entities  The method :只支持排序输入实体LINQ 的方法

        是使用skip()时没有排序时报的错误

    2. Validation failed for one or more entities. 'See EntityValidationErrors' property for more details :验证失败的一个或多个实体。更多细节见“entityvalidationerrors”属性

        修改数据字段的时候报的错误

        2.1 关闭ef的实体检查

    db.Configuration.ValidateOnSaveEnabled = false

        2.2 查看entityvalidationerrors具体那个些字段没有通过

    3. 当从数据库取出数据的时候对表中的数据项做扩张方法的时候,例如.ToString()报错。

        错误代码:

              var data = from d in Db.Members
                               where d.ID == User_Id
                               select new
                               {
                                   d.ID,
                                   d.Status
                               };
                    var result = from r in data
                                 select new
                                 {
                                     status = r.Status.Display()
                                 };

        修改后:

                   var data = from d in Db.Members
                               where d.ID == User_Id
                               select new
                               {
                                   d.ID,
                                   d.Status
                               };
                    var result = from r in data.ToList()
                                 select new
                                 {
                                     status = r.Status.Display()
                                 };        

    刚取出数据的时候IQueryable<>的类型,把它转换成List();ToList()

     4. An entity object cannot be referenced by multiple instances of IEntityChangeTracker:IEntityChangeTracker

         单独使用DbContext对象操作

            using (var db = new Context.Create())
                {
                    //把使用过的DbContext对象都释放掉
                }

     5. entity framework在使用一对一的关系,添加数据不报错,但是数据添加不进去

      错误的代码:

    /**
                 * shop:是主表
                 * shopCertificate:是shop的从关系(外键)
                 * 直接new出外键不会报错,但也添加不了数据
    * 表红的地方,进行外键绑定 * *
    */ using (var db = Context.Default) { var shop = new Shop() { Address = "qwe", Banance = "asd", Certificate = null, City = "zxcf", Description = "rty", District = "fgh", Logo = "vbn", Memo = "uio", Name = "jkl", Provence = "nm", Tax = 0.1m, Status = ShopStatus.Abnormal, Type = null }; var shopCertificate = new ShopCertificate() { BankAccountName = "", BankAccountNumber = "", BankName = "", BankNumber = "", CompanName = "", LegalPerson = "", LegalPersonIDCardNumber = "", LegalPersonIDCardPath = "", LicenseNumber = "", LicensePath = "", Status = CompanyStatus.Abnormal, Shop_Id = shop.ID, TaxCertificateNumber = "", TaxCertificatePath = "", }; db.ShopCertificates.Add(shopCertificate); db.Shops.Add(shop); db.SaveChanges(); }

      正确的做法

                using (var db = Context.Default)
                {
                    var shop = new Shop()
                    {
                        Address = "qwe",
                        Banance = "asd",
                        Certificate = null,
                        City = "zxcf",
                        Description = "rty",
                        District = "fgh",
                        Logo = "vbn",
                        Memo = "uio",
                        Name = "jkl",
                        Provence = "nm",
                        Tax = 0.1m,
                        Status = ShopStatus.Abnormal,
                        Type = null
                    };
                    //直接用shop的外键关系上再创建新的对象
    //调试可以看到result值为2 shop.Certificate
    = new ShopCertificate() { BankAccountName = "", BankAccountNumber = "", BankName = "", BankNumber = "", CompanName = "", LegalPerson = "", LegalPersonIDCardNumber = "", LegalPersonIDCardPath = "", LicenseNumber = "", LicensePath = "", Status = CompanyStatus.Abnormal, Shop_Id = shop.ID, TaxCertificateNumber = "", TaxCertificatePath = "", }; db.Shops.Add(shop); int result = db.SaveChanges(); }
  • 相关阅读:
    7. v-bind 绑定Class操作 【对象语法】
    7。 V-bind 绑定
    【离散化】
    【洛谷 1576】最小花费
    【洛谷 1078】文化之旅
    【POJ 2115】CLooooops
    【洛谷 1516】青蛙的约会
    【UOJ 270】电厂计划
    【UOJ 92】有向图的强联通分量
    【POJ 2186】Popular Cows
  • 原文地址:https://www.cnblogs.com/haosit/p/6877827.html
Copyright © 2020-2023  润新知