Code First 与现有数据库一对一映射列错误

Code First with existing database one to one mapping column error

我有一个现有的数据库,它包含以下两个 table:

我正在尝试使用流畅的映射从头开始使用数据库创建一个 EF Code First。

我配置了以下 dbContext:

public partial class EFContext : DbContext
{
    public EFContext()
        : base("name=DbContext")
    {
    }

    public virtual DbSet<Users> Users { get; set; }
    public virtual DbSet<Log> Log { get; set; }
    public virtual DbSet<Token> Tokens { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Configurations.Add(new UserConfiguration());
        modelBuilder.Configurations.Add(new LogConfiguration());
        modelBuilder.Configurations.Add(new TokenConfiguration());
    }
}

    public partial class Users
    {

        public int UserId { get; set; }
        public string Username { get; set; }
        public string Password { get; set; }
        public int Active { get; set; }
        public DateTime RegDate { get; set; }
        public virtual Token Token { get; set; }
     }



     public class Token
     {   
        public string TokenId { get; set; }
        public int UserId { get; set; }
        public string TokenValue { get; set; }
        public int Active { get; set; }
        public DateTime Fecalt { get; set; }
        public virtual Users User { get; set; }
     }

public class UserConfiguration : EntityTypeConfiguration<Users>
{
    public UserConfiguration() : base()
    {
        HasKey(p => p.UserId);

        Property(e => e.Username)
            .IsUnicode(false)
            .IsRequired()
            .HasMaxLength(50);

        Property(e => e.Password)
             .IsUnicode(false)
             .IsRequired()
             .HasMaxLength(50);

        Property(a => a.Active).IsRequired();
        Property(d => d.RegDate).IsRequired();
        HasOptional(u => u.Token).WithRequired(u => u.User);
    }
}

 public class TokenConfiguration: EntityTypeConfiguration<Token>
    {
        public TokenConfiguration()
        {
            HasKey(p => p.TokenId);
            Property(p => p.TokenId).HasMaxLength(50);
            Property(p => p.TokenValue).HasColumnName("Token").IsRequired().HasMaxLength(500);
            Property(p => p.Active).IsRequired();
            Property(p => p.Fecalt).IsRequired();
            ToTable("Tokens");
        }
    }

我有以下异常:

Invalid column name 'User_UserId'.\r\nInvalid column name 'User_UserId'.\r\nInvalid column name 'User_UserId'."

生成的查询是这样的(显然是错误的):

SELECT [Extent1].[UserId] AS [UserId], [Extent1].[Username] AS [Username], [Extent1].[Password] AS [Password], [Extent1].[Active] AS [Active], [Extent1].[RegDate] AS [RegDate], [Extent3].[TokenId] AS [TokenId], [Extent3].[UserId] AS [UserId1], [Extent3].[Token] AS [Token], [Extent3].[Active] AS [Active1], [Extent3].[Fecalt] AS [Fecalt], [Extent3].[User_UserId] AS [User_UserId] FROM [dbo].[Users] AS [Extent1] LEFT OUTER JOIN [dbo].[Tokens] AS [Extent2] ON [Extent1].[UserId] = [Extent2].[User_UserId] LEFT OUTER JOIN [dbo].[Tokens] AS [Extent3] ON [Extent1].[UserId] = [Extent3].[User_UserId]

查询如下:

 var query = from p in efContext.Users
                       .Include( p =>p.Token)
                        select p;

外键分配不当,左连接重复,但我不知道如何解决。

关系在用户中:

 HasOptional(u => u.Token).WithRequired(u => u.User);

registry user为1到0..1,user token可选,PK/FK关系为UserID

实现的一种方法是从 class Token 中删除 UserId 属性 并在 TokenConfiguration 中配置关系而不是 UserConfiguration 调用MapKey方法显式指定外键

public class Token
{
    public string TokenId { get; set; }
    public string TokenValue { get; set; }
    public int Active { get; set; }
    public DateTime Fecalt { get; set; }
    public virtual Users User { get; set; }
}

public class UserConfiguration : EntityTypeConfiguration<Users>
{
    public UserConfiguration()
        : base()
    {
        HasKey(p => p.UserId);

        Property(e => e.Username)
            .IsUnicode(false)
            .IsRequired()
            .HasMaxLength(50);

        Property(e => e.Password)
             .IsUnicode(false)
             .IsRequired()
             .HasMaxLength(50);

        Property(a => a.Active).IsRequired();
        Property(d => d.RegDate).IsRequired();

    }
}

public class TokenConfiguration : EntityTypeConfiguration<Token>
{
    public TokenConfiguration()
    {
        HasKey(p => p.TokenId);
        Property(p => p.TokenId).HasMaxLength(50);
        Property(p => p.TokenValue).HasColumnName("Token").IsRequired().HasMaxLength(500);
        Property(p => p.Active).IsRequired();
        Property(p => p.Fecalt).IsRequired();
        HasRequired(d => d.User).WithOptional(d => d.Token).Map(m => m.MapKey("UserId"));
        ToTable("Tokens");
    }
}

它与我的代码完美配合。