C# 字符串或二进制数据将被截断。删除数据时语句已终止
我有一个C# 字符串或二进制数据将被截断。删除数据时语句已终止,c#,sql,sql-server,entity-framework,C#,Sql,Sql Server,Entity Framework,我有一个PriceId,通过它我得到了一些PriceGaranties,我试图通过更新Price来删除PriceGaranties,并看到这个错误 代码如下: public void DeletePriceGaranties(int priceId) { var deletedPriceGaranties = context.PriceGaranties.Where(p => p.PriceId == priceId).ToList(); foreach (var pric
PriceId
,通过它我得到了一些PriceGaranties
,我试图通过更新Price
来删除PriceGaranties
,并看到这个错误
代码如下:
public void DeletePriceGaranties(int priceId)
{
var deletedPriceGaranties = context.PriceGaranties.Where(p => p.PriceId == priceId).ToList();
foreach (var priceGaranty in deletedPriceGaranties)
{
context.PriceGaranties.Remove(priceGaranty);
context.SaveChanges();
}
}
错误是:
字符串或二进制数据将被截断。声明已被修改
终止
更新
以下是模型:
public class Price
{
public int PriceId { get; set; }
public int ProductId { get; set; }
public int ClassId { get; set; }
public int ClassPartnerId { get; set; }
public int UserId { get; set; }
public decimal Cost { get; set; }
public string Url { get; set; }
public string Description { get; set; }
public DateTime LastUpdate { get; set; }
public bool Enable { get; set; }
public int CostTypeId { get; set; }
public int Value { get; set; }
public Class Class { get; set; }
public Product Product { get; set; }
public User User { get; set; }
public List<OldPrice> OldPrices { get; set; }
public List<PriceColor> PriceColors { get; set; }
public List<PriceGaranty> PriceGaranties { get; set; }
public ClassPartner ClassPartner { get; set; }
}
public class PriceGaranty
{
public int PriceGarantyId { get; set; }
public int PriceId { get; set; }
public int GarantyId { get; set; }
public Comparing.Model.Price.Price Price { get; set; }
}
公共类价格
{
public int PriceId{get;set;}
public int ProductId{get;set;}
public int ClassId{get;set;}
public int ClassPartnerId{get;set;}
public int UserId{get;set;}
公共十进制成本{get;set;}
公共字符串Url{get;set;}
公共字符串说明{get;set;}
公共日期时间上次更新{get;set;}
公共bool启用{get;set;}
public int CostTypeId{get;set;}
公共int值{get;set;}
公共类{get;set;}
公共产品产品{get;set;}
公共用户{get;set;}
公共列表价格{get;set;}
公共列表PriceColors{get;set;}
公共列表价格标签{get;set;}
公共类伙伴类伙伴{get;set;}
}
公费舱
{
public int PriceGarantyId{get;set;}
public int PriceId{get;set;}
公共int GarantyId{get;set;}
public.Model.Price.Price价格{get;set;}
}
并且所有字符串类型都是nvarchar(MAX)。您已指定数据库中字符串的长度比您试图添加到其中的长度短。例如,您可能已将数据库中的varchar长度设置为10,但要添加的字符串为11、15、20等字符。数据库不知道该如何处理它,所以它只会删除多余的字符。尝试将数据库中的字段长度设置为更大的长度
您已将数据库中字符串的长度指定为比要添加的长度短。例如,您可能已将数据库中的varchar长度设置为10,但要添加的字符串为11、15、20等字符。数据库不知道该如何处理它,所以它只会删除多余的字符。尝试将数据库中的字段长度设置为更大的长度
您能告诉我们这些类的属性吗?有一个字符串属性,您需要更改该属性的映射配置以获得更大长度的字符串。请确保
PriceGaranties
中每个属性的数据长度与数据库表中的数据长度相同。您已将数据库中字符串的长度指定为短于您试图添加到其中的长度。例如,您可能已将数据库中的varchar长度设置为10,但要添加的字符串是11、15、20等字符。数据库不知道该如何处理它,所以它只会删除多余的字符。尝试将数据库中的字段长度设置为更大的长度。也不是为了对拼写一丝不苟,而是你把担保书拼错了。除非这个词有别的意思??哈哈。@KyleT当它在foreach循环中删除PriceGaranties时,问题就出现了,但我将price的所有字符串类型都设置为nvarchar(MAX),但什么都没有发生!在sql server中的“工具”菜单下打开sql探查器。打开后,在执行SQL脚本的代码上放置一个断点,并单步执行代码,直到脚本运行。这项技术将告诉您SQL脚本中的代码在哪里被破坏。你能为我们截图一下你的数据库模式吗?这样我们就可以看到哪些数据可能被截断。您能告诉我们类的属性吗?有一个字符串属性,您需要更改该属性的映射配置以获得更大长度的字符串。请确保PriceGaranties
中每个属性的数据长度与数据库表中的数据长度相同。您已将数据库中字符串的长度指定为短于您试图添加到其中的长度。例如,您可能已将数据库中的varchar长度设置为10,但要添加的字符串是11、15、20等字符。数据库不知道该如何处理它,所以它只会删除多余的字符。尝试将数据库中的字段长度设置为更大的长度。也不是为了对拼写一丝不苟,而是你把担保书拼错了。除非这个词有别的意思??哈哈。@KyleT当它在foreach循环中删除PriceGaranties时,问题就出现了,但我将price的所有字符串类型都设置为nvarchar(MAX),但什么都没有发生!在sql server中的“工具”菜单下打开sql探查器。打开后,在执行SQL脚本的代码上放置一个断点,并单步执行代码,直到脚本运行。这项技术将告诉您SQL脚本中的代码在哪里被破坏。你能为我们截图一下你的数据库模式吗?这样,我们可以看到哪些数据可能被截断。表上的“您的数据库”标题应真正读取“您的数据库表”我的错误。感谢您花费时间。表上的“您的数据库”标题应真正读取“您的数据库表”我的错误。感谢您花费时间。