简体   繁体   中英

Entity Framework Code First generates two foreign key columns in database when creating a 1:n relationship

I want to create an 1:n relationship, based on the naming conventions for creating relationships, with the Entity Framework. I'm using codefirst. Entity Framework ignores the conventions and alwas creates two foreign key columns, instead of only one:

  • StreamWorkerUser_Id (i want only this column)
  • StreamWorkerUser_Id1

The configuration class is configured like following:

AutomaticMigrationsEnabled = true;
AutomaticMigrationDataLossAllowed = true;

I'm using the Microsoft.Identity Framework, with custom classes for all models.

My DbContext class is as simple as following.

public class StreamWorkerDataContext : IdentityDbContext<StreamWorkerUser, StreamWorkerRole, int, StreamWorkerUserLogin, StreamWorkerUserRole, StreamWorkerUserClaim>
{
    public DbSet<Task> Tasks { get; set; }
}

The Task model:

public class Task : StreamWorkerEntity
{
    public StreamWorkerUser StreamWorkerUser { get; set; }
}

The StreamWorkerUser model:

public class StreamWorkerUser : IdentityUser<int, StreamWorkerUserLogin, StreamWorkerUserRole, StreamWorkerUserClaim>
    {
        public virtual List<Task> Tasks { get; set; } // also tested ICollection
    }

The Task class inherits from this base type, here are also two properties of type StreamWorkerUser defined (But these columns generated correctly in the database.):

public class StreamWorkerEntity
{
    [Key]
    public int Id { get; set; }
    public DateTimeOffset Created { get; set; }
    public DateTimeOffset Updated { get; set; }
    public StreamWorkerUser Creator { get; set; }
    public StreamWorkerUser LastEditor { get; set; }
}

Is it possible that, the properties Creator and LastEditor disturbing the database migration?

Do i understand the conventions correctly?

Much more information:

  • Entity Framework Version 6.1.3
  • Microsoft.AspNet.Identity.Core 2.2.1
  • Microsoft.AspNet.Identity.EntityFramework 2.2.1
  • ASP.NET 4 MVC 5
  • SQL Server in Azure Cloud
  • C# 6.0
  • .NET Framework 4.5.2
  • All of this code is in a class libary

So you might think that EF in this situation:

 public StreamWorkerUser Creator { get; set; }
 public StreamWorkerUser LastEditor { get; set; }

will create creator_id and lasteditor_id, but it doesn't, because EF took class name and made StreamWorkerUser_Id, and StreamWorkerUser_Id1, because there were StreamWorkerUser_Id already used. By default, if you make relation by class(not by int field), EF generates its column name depending on the class name(not by field name).

If you want to specify column names for those FK you can do :

    public int CreatorId { get; set; } //this field name will be column name
    [ForeignKey("StreamWorkerUser")]
    public StreamWorkerUser Creator { get; set; }

    public int EditorId { get; set; } //this field name will also be column name
    [ForeignKey("StreamWorkerUser")]
    public StreamWorkerUser Editor { get; set; }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM