C# 实例化新实体时实体框架生成索引错误
为了解决我的问题,我使用了C#VS 2017+EF 6(DB优先)+SQL Server 请让我解释一下这个问题:我正在实例化一个类以向其中添加一条记录。然而,在这个特定的类中,当我实例化时,我立即得到一个超出范围的索引错误。这是类型为C# 实例化新实体时实体框架生成索引错误,c#,visual-studio,entity-framework,C#,Visual Studio,Entity Framework,为了解决我的问题,我使用了C#VS 2017+EF 6(DB优先)+SQL Server 请让我解释一下这个问题:我正在实例化一个类以向其中添加一条记录。然而,在这个特定的类中,当我实例化时,我立即得到一个超出范围的索引错误。这是类型为System.ArgumentOutOfRangeException的错误 详情: 我没有添加值,只是实例化了实体 出于测试目的,我从相关表中删除了所有关系,问题仍然存在 我已经删除了该表,并在SQL和EDMX中重新创建了它 有人经历过这种情况吗?有什么建议吗 我
System.ArgumentOutOfRangeException
的错误
详情:
using (coletasEntities ctx = new coletasEntities())
{
// error happens on line below
coleta_nota_problema erro = new coleta_nota_problema();
erro.descricao = msg.Substring(0, 250);
erro.id_coleta_nota = id;
ctx.coleta_nota_problema.Add(erro);
ctx.SaveChanges();
}
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated from a template.
//
// Manual changes to this file may cause unexpected behavior in your application.
// Manual changes to this file will be overwritten if the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
namespace Coletas
{
using System;
using System.Collections.Generic;
public partial class coleta_nota_problema
{
public int id_coleta_nota_problema { get; set; }
public int id_coleta_nota { get; set; }
public string descricao { get; set; }
}
}
使用(coletasEntities ctx=new coletasEntities())
{
//错误发生在下面的第行
coleta_nota_problema erro=新的coleta_nota_problema();
erro.descripao=msg.Substring(0,250);
erro.id_coleta_nota=id;
ctx.coleta_nota_problema.Add(erro);
ctx.SaveChanges();
}
//------------------------------------------------------------------------------
//
//此代码是从模板生成的。
//
//手动更改此文件可能会导致应用程序出现意外行为。
//如果重新生成代码,将覆盖对此文件的手动更改。
//
//------------------------------------------------------------------------------
名称空间学院
{
使用制度;
使用System.Collections.Generic;
公共部分类coleta_nota_问题
{
公共整数id_coleta_nota_problema{get;set;}
public int id_coleta_nota{get;set;}
公共字符串描述符{get;set;}
}
}
要隔离问题,请先尝试分配硬编码值,看看错误是否消失:
erro.descricao = "hello"; // any arbitrary text
erro.id_coleta_nota = 1000; // any arbitrary integer unique if this is the key
erro.id_coleta_nota_problema = 1000; // any arbitrary integer
正如你在帖子中的评论所提到的,以下几行很可能是罪魁祸首:
erro.descricao = msg.Substring(0, 250);
要隔离问题,请先尝试指定硬编码值,然后查看错误是否消失:
erro.descricao = "hello"; // any arbitrary text
erro.id_coleta_nota = 1000; // any arbitrary integer unique if this is the key
erro.id_coleta_nota_problema = 1000; // any arbitrary integer
正如你在帖子中的评论所提到的,以下几行很可能是罪魁祸首:
erro.descricao = msg.Substring(0, 250);
我认为您的问题更可能是调用
Substring()
。我猜msg
没有那么长。@Crowcoder,它肯定不是子字符串。错误发生在紧跟实体实例之后的行上,甚至不处理序列行。如果我从子字符串中删除该行,问题将继续。好的,那么我们可以看到coelta\u nota\u problema
的构造函数吗?异常的堆栈跟踪是什么?这个类很可能有一个部分扩展,其构造函数的代码有问题。您的CALS中的KEY属性是什么?您也可以给我们看一下您正在使用的数据(id和msg)。此外,您还可以使用DBContext.Database.Log属性在DBContext级别启用日志记录,该属性记录所有进入数据库的INSERT语句。另外,您可以运行sql profiler并查看发送到数据库的INSERT语句。我认为,调用Substring()。我猜msg
没有那么长。@Crowcoder,它肯定不是子字符串。错误发生在紧跟实体实例之后的行上,甚至不处理序列行。如果我从子字符串中删除该行,问题将继续。好的,那么我们可以看到coelta\u nota\u problema
的构造函数吗?异常的堆栈跟踪是什么?这个类很可能有一个部分扩展,其构造函数的代码有问题。您的CALS中的KEY属性是什么?您也可以给我们看一下您正在使用的数据(id和msg)。此外,您还可以使用DBContext.Database.Log属性在DBContext级别启用日志记录,该属性记录所有进入数据库的INSERT语句。另外,您可以运行sql profiler并查看发送到数据库的INSERT语句我更改了行以修复错误:if(msg.lenth>250)msg=msg.substring(0250)我更改了行以修复错误:if(msg.lenth>250)msg=msg.substring(0250)