I am trying to modify the source code of an existing application to use First Entity Framework Code. I used the built-in tool in Visual Studio 2015 to create POCO classes based on my existing database. This worked fine for the most part, with the exception of two classes, with a one to zero or one relationship. These are my (simplified) classes:
public class Login { [Key] [DatabaseGenerated(DatabaseGeneratedOption.Identity)] public int Id { get; set; } public int TeamMemberId { get; set; } public virtual TeamMember TeamMember { get; set; } } public class TeamMember { [Key] [DatabaseGenerated(DatabaseGeneratedOption.Identity)] public int Id { get; set; } public virtual Login Login { get; set; } }
With the following configuration:
public class LoginTypeConfiguration : EntityTypeConfiguration<Login> { public LoginTypeConfiguration() { this.HasRequired(e => e.TeamMember) .WithOptional(e => e.Login); this.Property(e => e.TeamMemberId) .HasColumnName("TeamMember_Id"); } }
This results in the following migration:
CreateTable( "dbo.Logins", c => new { Id = c.Int(nullable: false, identity: true), TeamMember_Id = c.Int(nullable: false), }) .PrimaryKey(t => t.Id) .ForeignKey("dbo.TeamMembers", t => t.Id) .Index(t => t.Id);
For some reason, EF creates a foreign key on [Logins].[Id] instead of [Logins].[TeamMember_Id] . I already tried to decorate my navigation property with the ForeignKey attribute, but that didn't work. Is there any way to get it to create a foreign key on [Logins].[TeamMember_Id] instead?
source share