When generating migrations for the following class:
```c#
public class User
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
[Key]
public virtual Guid Id { get; set; }
public virtual string FirstNames { get; set; }
public virtual string LastName { get; set; }
public virtual string EmailAddress { get; set; }
public virtual int Status { get; set; }
public virtual DateTime LastUpdated { get; set; }
public virtual DateTime Created { get; set; }
public virtual User CreatedBy { get; set; }
public virtual User LastUpdatedBy { get; set; }
public virtual bool IsDeleted { get; set; }
}
The following mapping is created:
```c#
migrationBuilder.CreateTable(
name: "User",
columns: table => new
{
Id = table.Column<Guid>(nullable: false),
FirstNames = table.Column<string>(nullable: true),
LastName = table.Column<string>(nullable: true),
EmailAddress = table.Column<string>(nullable: true),
Status = table.Column<int>(nullable: false),
LastUpdated = table.Column<DateTime>(nullable: false),
Created = table.Column<DateTime>(nullable: false),
CreatedById = table.Column<Guid>(nullable: true),
IsDeleted = table.Column<bool>(nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_User", x => x.Id);
table.ForeignKey(
name: "FK_User_User_CreatedById",
column: x => x.CreatedById,
principalTable: "User",
principalColumn: "Id",
onDelete: ReferentialAction.Restrict);
});
As you can see, the LastUpdatedBy column is not being mapped in the migration, whereas the CreatedBy column is. These two columns are seemingly the same to me, so why is one being mapped and not the other?
EF Core version: 2.1.3
Database Provider: Microsoft.EntityFrameworkCore.SqlServer
Operating system: MacOS
IDE: VS for Mac 2017
Thanks :).
@Jaffacakes82 What does the User class look like and do you have any configuration in OnModelCreating?
@ajcvickers the User class is at the top of my previous comment.
My OnModelCreating method just has a couple of table name bindings and a many to many mapping:
```c#
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity
modelBuilder.Entity
modelBuilder.Entity
modelBuilder.Entity
modelBuilder.Entity<RolePermission>()
.HasKey(t => new { t.RoleId, t.PermissionId });
}
```
CreatedBy & LastUpdatedBy are not scalar properties which would have corresponding columns. They are navigations. Since they are defined on User and also target User type, together they form a self-referencing foreign key navigation pair. One of those navigation CreatedBy is picked as dependent to principal navigation hence the shadow FK created is named CreatedById and there is not LastUpdatedById (since there is nothing to map to it).
If your intention is to have 2 foreign keys, 1 for each navigation then you need to configure them manually in fluent API to tell EF that, your navigations does not have inverse navigations and don't combine them in single FK.
Most helpful comment
CreatedBy&LastUpdatedByare not scalar properties which would have corresponding columns. They are navigations. Since they are defined onUserand also targetUsertype, together they form a self-referencing foreign key navigation pair. One of those navigationCreatedByis picked as dependent to principal navigation hence the shadow FK created is namedCreatedByIdand there is notLastUpdatedById(since there is nothing to map to it).If your intention is to have 2 foreign keys, 1 for each navigation then you need to configure them manually in fluent API to tell EF that, your navigations does not have inverse navigations and don't combine them in single FK.