我有一个现有数据库,它包含以下两个表:
我试图创建一个EF代码首先与数据库从头开始使用流畅的映射。
我配置了以下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]作为[UserId],[Extent1].[Username]作为[Username],[Extent1].[Password]作为[Password],[Extent1].[Active]作为[Active],[Extent1].[RegDate]作为[RegDate],[Extent3].[TokenId]作为[TokenId],[Extent3].[UserId]作为[UserId1],[Extent3].[Active]作为[Active1],[Extent3].[Fecalt]作为[Fecalt],[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。
一种实现方法是从类token
中删除userid
属性,并在tokenconfiguration
中配置关系,而不是调用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");
}
}
它和我的代码运行得很完美。