得到错误“不能添加已经存在的实体".在不使用表的情况下将表的值插入DB的情况下 [英] Getting the error "cannot add an entity that already exists." while inserting a table's value in to DB without procedure
本文介绍了得到错误“不能添加已经存在的实体".在不使用表的情况下将表的值插入DB的情况下的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用Linq to sql将记录列表插入到DB表中,如下所示:
I am inserting a list of records to the DB table with Linq to sql like this:
//my DataContext Class
using (VTMMedicalDBDataContext objVTMMedicalDBDataContext = new VTMMedicalDBDataContext())
{
ReadOnlyCollection<TimeZoneInfo> objTimeZones = null;
objTimeZones = TimeZoneInfo.GetSystemTimeZones();
if (objTimeZones.Count > 0)
{
//List<TimeZoneMaster> listTimeZones = new List<TimeZoneMaster>();
TimeZoneMaster objTimeZoneMaster = new TimeZoneMaster();
foreach (var timezone in objTimeZones.ToList())
{
objTimeZoneMaster.TimeZoneName = timezone.DisplayName;
var localName = timezone.DisplayName;
objTimeZoneMaster.TimeZoneOffsetInMinutes = Convert.ToInt32(timezone.BaseUtcOffset.TotalMinutes);
objVTMMedicalDBDataContext.TimeZoneMasters.InsertOnSubmit(objTimeZoneMaster);
objVTMMedicalDBDataContext.SubmitChanges();
}
}
}
我有一个主键,但是我已经将它作为AutoGeneratd在DBML Nullable中设置为true,而将其设置为false.仍然无法摆脱它...请提出一些建议.
I have a primary key but I have already made it as AutoGeneratd as true in DBML Nullable as false.Still I am not able to get rid of it...Please suggest some way around.
推荐答案
最后,在经历了很多麻烦之后,我有了一个新概念的答案.
Finally After Much hassle I got my answer with a new concept..
对于所有面临类似问题的人,这里有一个明确的解决方案:
您需要创建"Table"类的对象(要在DB中更新的对象),在循环( *中,对于每个,for或任何其他对象.)中,这样就不会更新相同的记录,并且每个对象都使用不同的内存位置(因为本地实例会在仅循环.) *
这篇关于得到错误“不能添加已经存在的实体".在不使用表的情况下将表的值插入DB的情况下的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文