1

I ask a similar question here, but this is a different situation that results in the same error message.

I am updating an NON-index, NON-unique property, PageNumber.

And I am receiving the following error

OleDbException: The changes you requested to the table were not successful because they would create duplicate values in the index, primary key, or relationship. Change the data in the field or fields that contain duplicate data, remove the index, or redefine the index to permit duplicate entries and try again.

public void DoRenumberPages(object blah)
{


    var hiddenPages = projectDB.Pages.AsEnumerable().Where(x => !IsVisibleDrawing(x.DrawingType) && x.DrawingType != 3001).ToList();

    for (int i = 0; i < hiddenPages.Count(); i++)
    {                
        hiddenPages[i].PageNumber = i + 1000;
    }

    var TOCPages = projectDB.Pages.AsEnumerable().Where(x => x.DrawingType == 3001).OrderBy(x => x.BookNumber).ToList();

    for (int i = 0; i < TOCPages.Count(); i++)
    {               
        TOCPages[i].PageNumber = i + 1;
    }

    var visiblePagesNotTOC = projectDB.Pages.AsEnumerable().Where(x => IsVisibleDrawing(x.DrawingType) && x.DrawingType != 3001).OrderBy(x => x.BookNumber).ToList();

    for (int i = 0; i < visiblePagesNotTOC.Count(); i++)
    {                
        visiblePagesNotTOC[i].PageNumber = i + TOCPages.Count() + 1;
    }


    projectDB.SaveChanges();

    RenumberPages.EnableExecute();

}

Page Model Class

[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; }        
}

ETA:

I have change public int BookNumber { get; set; } to public int? BookNumber { get; set; } This doesn't solve the issue.

TheColonel26
  • 2,618
  • 7
  • 25
  • 50
  • If the primary key is an autonumbering field, the only other unique index is on BookNumber so you are creating duplicated BookNumber values. In this code is not clear where it is happening. There could be an issue related to BookNumber not nullable in the model and nullable on DB. – bubi Jun 30 '17 at 06:42
  • I have change `public int BookNumber { get; set; }` to `public int? BookNumber { get; set; }` This doesn't solve the issue. I can send you source code and access database if that will help. – TheColonel26 Jun 30 '17 at 15:30
  • If you send me a compilable example (with also data) I can check it – bubi Jul 01 '17 at 06:56
  • @bubi: Here is a zip with the solution, and databse. Just run the Test project. https://1drv.ms/u/s!AsA1Mz52V5ZYg6RVOUldFiCekWrt_w – TheColonel26 Jul 02 '17 at 05:17
  • I think you developed the project with VS 2017... Anyway, I already tougth to install it but I need some days... – bubi Jul 04 '17 at 15:12
  • I write this in a normal answer becouse here there is not enough space... – bubi Jul 04 '17 at 18:04

1 Answers1

1

Ok, I could start the project.
The problem is the one I wrote here duplicate values in the index, primary key, or relationship. If you enable query printing setting JetEntityFrameworkProvider.JetConnection.ShowSqlStatements = true; somewhere (I did it in your Test.Program.Main) you can see the statements that EF runs. The first update query is

update [Content]
set [Page] = @p0
where ([Counter] = @p1)

@p0 = 3
@p1 = 2

If you look at the database, the time you run the query the Page = 3 is already contained in the record with Counter = 3. You can't solve this problem also inside a transaction and also with other (next to all) DBMSs you have the same problem.

The only solution (if you need the unique index on Page) is to update in 2 different SaveChanges. For example:
1. Set Page = null then SaveChanges()
2. Set Page = number then SaveChanges()

Microsoft Access permits duplicated null values in unique indexes. If you will permit to use different databases you could have problems.

bubi
  • 6,414
  • 3
  • 28
  • 45
  • The page column is not listed as unique or an index in the database, but I still receive this error. If you open the database file in access you can see this. The password for the database is "SEEME" by the way – TheColonel26 Jul 04 '17 at 23:45
  • 1
    The problem is on the indexes Name and NameDesc. They are both composite unique indexes. *EDIT*: you can try to make the same thing in Microsoft Access and you'll receive the same error. – bubi Jul 05 '17 at 05:48
  • What table is Name and NameDesc in? it is not in the Content table. – TheColonel26 Jul 06 '17 at 00:34
  • 1
    They are 2 unique indexes on the Content table. They both include Page column. – bubi Jul 06 '17 at 08:13
  • 1
    Ohh ok, that clarifies things. Thank you very much for your help especially since it turned out to be jet/access related and just EF-Jet-Provider related. – TheColonel26 Jul 06 '17 at 16:45
  • Post Note: I finally found where the Indexes are listed in MS Access. Only some of them were showing on the property view, which was confusing me. – TheColonel26 Jul 07 '17 at 00:49