Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove foreign key added by convention in Entity Framework Core

I have two entities:

class User
{
    public Guid UserId { get; set; }
}

class ActionLog
{
    public Guid ActionLogId { get; set; }
    public Guid UserId { get; set; }
    public User User {get; set;}
}

When I add those to EF Core data context a foreign key is generated by convention on ActionLog.UserId, but I would like just to log UserId without a relation. I don't want cascade deletes and key checks. How could I remove that convention?

like image 409
Vladimir Perevalov Avatar asked Sep 15 '25 10:09

Vladimir Perevalov


1 Answers

This code removes foreign key relation with User as well as keeps UserId column without relation attached to User. Tested with ef core 6.

modelBuilder.Entity<ActionLog>().Ignore(e => e.User);
like image 199
Namig Hajiyev Avatar answered Sep 17 '25 01:09

Namig Hajiyev