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.

Wrong parameter sent when setting a foreign key

See original GitHub issue

EF Core version: 2.1.2 Database Provider: Npgsql.EntityFrameworkCore.PostgreSQL v2.1.1.1

Using the following entity:

[Table("caps_user_shares")]
internal class CapsUserShareEntity
{
    [Column("caps_id")]
    public Guid CapsId { get; set; }
    [Column("user_id")]
    public Guid UserId { get; set; }
    [Column("location")]
    [Required] public Point Location { get; set; }

    public CapsEntity Caps { get; set; }
    public UserAccountEntity User { get; set; }
}

and the following foreign key rule:

modelBuilder.Entity<CapsUserShareEntity>().HasOne(s => s.User).WithMany().HasForeignKey(s => s.UserId);

I tried to add such an entity and set its UserId to Guid.Empty which corresponds to a (special) user that exists in the DB:

Context.CapsUserShares.Add(new CapsUserShareEntity
{
    CapsId = @event.EntityId,
    UserId = Guid.Empty,
    Location = PointFromLocationModel(@event.Location)
});

This failed with a violation of the foreign key constraint I described above. Tracing the SQL statements with EnableSensitiveDataLogging, I saw that the parameter sent for the user_id column was not Guid.Empty but some other Guid that I couldn’t match with anything else in the DB (so presumably the result of a Guid.NewGuid()).

The workaround I found was to set the User property to that special user I had to retrieve first:

var publicUser = await Context.UserAccounts.FirstOrDefaultAsync(a => a.Id == Guid.Empty);
...
Context.CapsUserShares.Add(new CapsUserShareEntity
{
    CapsId = @event.EntityId,
    User = publicUser,
    Location = PointFromLocationModel(@event.Location)
});

This approach does result in the right parameter being sent.

My intuition is that Guid.Empty being default(Guid), it is not considered as a set value by EF?

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:10 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
ThomasWeisscommented, Aug 31, 2018

@ajcvickers Thanks a lot for that clarification. For those running in the same issue and looking at my repro, the problem got solved by applying the [DatabaseGenerated(DatabaseGeneratedOption.None)] attribute to the Id of UserEntity.

0reactions
ajcvickerscommented, Aug 30, 2018

@ThomasWeiss Yes, the DatabaseGeneratedOption needs to go on the primary key–sorry I wasn’t clear about this. This is because when generating key values it is very unusual that a key value should be generated directly for an FK property, since it would not match any existing PK. Instead, PK values typically get their value via propagation from the primary key value, rather than having a new value generated.

Read more comments on GitHub >

github_iconTop Results From Across the Web

mysql Foreign key constraint is incorrectly formed error
The order in which I was creating tables was wrong. I was trying to reference a key from a table that was not...
Read more >
3 common foreign key mistakes (and how to avoid them)
Dangling foreign keys 3. Not creating foreign key indexes Bonus: Not using foreign keys Key takeaway: think before you CREATE TABLE.
Read more >
Foreign Key constraint error when inserting a record
Hello all,. I'm having an issue when it comes to inserting a record into a table that has a foreign key link.
Read more >
Wrong foreign key reference when creating composite ...
The issue. Extending the primary key of the IdentityUserRole class with another column fails. When the foreign key constraint is added for the ......
Read more >
Changing Foreign Keys and Navigations - EF Core
How to change relationships between entities by manipulating foreign keys and navigations.
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