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

时间:2015-04-06 11:20:20

标签: c# entity-framework entity-framework-6 sql-server-2014-express

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

enter image description here enter image description here

我正在尝试使用流畅的映射从头创建一个带有数据库的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");
        }
    }

我有以下例外:

  

无效的列名称'User_UserId'。\ r \ n无效的列名称   'User_UserId'。\ r \ n无效的列名'User_UserId'。“

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

  

选择       [Extent1]。[UserId] AS [UserId],       [Extent1]。[用户名] AS [用户名],       [Extent1]。[密码] AS [密码],       [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]。[用户] AS [Extent1]       LEFT OUTER JOIN [dbo]。[标记] AS [Extent2] ON [Extent1]。[UserId] = [Extent2]。[User_UserId]       LEFT OUTER JOIN [dbo]。[标记] 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);

注册表用户是1到0..1,用户令牌是可选的,PK / FK关系是UserID。

1 个答案:

答案 0 :(得分:0)

实现的一种方法是从类UserId中删除Token属性,并在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");
    }
}

它与我的代码完美配合。

相关问题