使用JetentityFrameWorkProvider和MS Access/Jet文件我正在更新实体的值,并且保存更改时会收到以下错误。
oledbexception:您要求对表的更改未成功,因为它们会在索引,主键或关系中创建重复值。更改包含重复数据的字段或字段中的数据,删除索引或重新定义索引以允许重复条目并重试。
这是我的代码
var pagesList = projectDB.Pages.OrderBy(x => x.PageNumber).ToList();
for(int i = 0; i < pagesList.Count(); i++)
{
pagesList[i].BookNumber = i+30;
}
foreach(var blah2 in pagesList.OrderBy(x => x.Id))
{
System.Console.WriteLine($" {{ID:{blah2.Id}, BN:{blah2.BookNumber}, PN:{blah2.PageNumber}}}");
}
projectDB.SaveChanges();
这是我的代码输出,因为您可以看到没有重复索引或重复的biknumber(索引且唯一(
{ID:2, BN:58, PN:5}
{ID:3, BN:59, PN:6}
{ID:6, BN:62, PN:11}
{ID:7, BN:65, PN:21}
{ID:20, BN:64, PN:13}
{ID:21, BN:66, PN:22}
{ID:25, BN:67, PN:23}
{ID:29, BN:68, PN:24}
{ID:78, BN:35, PN:1}
{ID:79, BN:45, PN:2}
{ID:108, BN:34, PN:1}
{ID:132, BN:73, PN:41}
{ID:177, BN:33, PN:1}
{ID:291, BN:74, PN:42}
{ID:318, BN:32, PN:1}
{ID:319, BN:42, PN:2}
{ID:320, BN:48, PN:3}
{ID:340, BN:31, PN:1}
{ID:341, BN:41, PN:2}
{ID:342, BN:50, PN:3}
{ID:343, BN:53, PN:4}
{ID:344, BN:55, PN:5}
{ID:345, BN:60, PN:6}
{ID:346, BN:61, PN:7}
{ID:452, BN:71, PN:32}
{ID:469, BN:63, PN:12}
{ID:510, BN:72, PN:39}
{ID:520, BN:43, PN:2}
{ID:524, BN:75, PN:43}
{ID:533, BN:70, PN:31}
{ID:539, BN:69, PN:25}
{ID:610, BN:30, PN:1}
{ID:611, BN:36, PN:1}
{ID:612, BN:46, PN:2}
{ID:613, BN:37, PN:1}
{ID:614, BN:38, PN:1}
{ID:615, BN:44, PN:2}
{ID:616, BN:51, PN:3}
{ID:617, BN:52, PN:4}
{ID:618, BN:56, PN:5}
{ID:619, BN:40, PN:1}
{ID:620, BN:39, PN:1}
{ID:621, BN:47, PN:2}
{ID:622, BN:49, PN:3}
{ID:623, BN:54, PN:4}
{ID:624, BN:57, PN:5}
eta:
模型
[Table("Content")]
public class Page
{
//** Primary Key
[Column("Counter")]
public int Id { get; set; }
public int ProjectCounter { get; set; }
public short Version { get; set; }
public short Revision { get; set; }
public bool Locked { get; set; }
public int DrawingType { get; set; }
//** Forign Key?
public int DeviceLocationCounter { get; set; }
//** Forign Key?
public int FolderID { get; set; }
[Column("Page")]
public int PageNumber { get; set; }
//** Indexed, Unique
public int BookNumber { get; set; }
public string PageIndex { get; set; }
//** Product
//** DrawingObject is not here
public bool Update { get; set; }
public short Flag { get; set; }
}
没有模型很难理解问题,但我认为您已经在BookNumber
上定义了唯一的索引。
ef运行每个更新的更新语句(每个页面(。
在某个时刻,在DB上(不在内存中(,有两个具有相同书记的实体。
在交易中,您可能会遇到相同的问题,因此解决方案是通过两个不同的步骤更新书籍
var pagesList = projectDB.Pages.OrderBy(x => x.PageNumber).ToList();
for (int i = 0; i < pagesList.Count; i++)
pagesList[i].BookNumber = null;
projectDB.SaveChanges();
for (int i = 0; i < pagesList.Count; i++)
pagesList[i].BookNumber = i + 30;
foreach (var blah2 in pagesList.OrderBy(x => x.Id))
Console.WriteLine("{{ID:{0}, BN:{1}, PN:{2}}}", blah2.Id, blah2.BookNumber, blah2.PageNumber);
projectDB.SaveChanges();