question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

EF Core 1.1 generating unique index on FK one-to-one

See original GitHub issue

I’m using EF Core 1.1 and created my one-to-one relationships, an example:

EntityTypeBuilder<Account>
   .HasOne(a => a.Customer)
   .WithOne(a => a.Account)
   .HasForeignKey<Account>(a => a.CustomerId)

Now using Add-Migration on my DbContext it generates the following:

migrationBuilder.CreateIndex(
   name: "IX_Accounts_CustomerId",
   table: "Accounts",
   column: "CustomerId",
   unique: true);

Is there a way to specify in Fluent to remove the uniqueness from the index? I don’t mind the index being generated, but for my purposes it can not be unique. The reason is that these tables are auditable so there can be duplicate data (outside of the PK). Hence records can contain the same FK id if there were no changes to that FK entity (but maybe changes elsewhere in the record).

TL;DR Any way of making the index non-unique in Fluent for one-to-one FKs?

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:19 (9 by maintainers)

github_iconTop GitHub Comments

0reactions
ishakdemircommented, Sep 3, 2020

@ajcvickers There are some open source EF-based auditing frameworks out there that, in essence, apply the same concepts to what I just described. For example: https://www.nuget.org/packages/TrackerEnabledDbContext

However none that I’ve researched are EF Core compatible and I do need a little finer control of things hence they usually don’t work out for my use cases. But the concepts are very similar.

Really EF Core doesn’t need to do anything special for auditing. I just need that 1:1 FK to be a non-unique index. I tried a while ago to not define the 1:1 relationship for that audit dbcontext but it generated one anyway (I believe it’s called shadowing)… so I just decided to explicitly define them myself. That’s why I suggested earlier if we could turn off the uniqueness-- or maybe even the index in general-- when defining the 1:1 relationship, e.g.

EntityTypeBuilder<Account>
   .HasOne(a => a.Customer)
   .WithOne(a => a.Account)
   .HasForeignKey<Account>(a => a.CustomerId)
   .HasIndex(false)

But it would be cool if you could build an add-on to EF Core that does this auditing OOTB… I’m sure lots of people would use it 😉

thank you

Read more comments on GitHub >

github_iconTop Results From Across the Web

EF Core 1.1 generating unique index on FK one-to-one
Now using Add-Migration on my DbContext it generates the following: migrationBuilder.CreateIndex( name: "IX_Accounts_CustomerId", table: " ...
Read more >
Indexes - EF Core
Configuring indexes in an Entity Framework Core model. ... Composite index; Index uniqueness; Index sort order; Index name; Index filter ...
Read more >
Generate a composite unique constraint/index, in EF Core ...
[Solved]-Generate a composite unique constraint/index, in EF Core-Entity ... can use (at least in ef core 1.1.0) the string based hasindex method overload...
Read more >
EF Core One-to-one Relationships - C# Tutorial
EF Core creates a unique index on the EmployeeId column of the EmployeeProfiles table to ensure that one row in the Employees table...
Read more >
dotConnect for SQLite History
The bug with generating fluent mapping for the one-to-one association by the ... GridControl is fixed; The bug with creating indices by EF...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found