C# 先用现有数据库编码一对一映射列错误
我有一个现有数据库,其中包含以下两个表: 我正在尝试使用fluent mapping从头开始使用数据库创建EF代码 我已配置以下dbContext:C# 先用现有数据库编码一对一映射列错误,c#,entity-framework,entity-framework-6,sql-server-2014-express,C#,Entity Framework,Entity Framework 6,Sql Server 2014 Express,我有一个现有数据库,其中包含以下两个表: 我正在尝试使用fluent mapping从头开始使用数据库创建EF代码 我已配置以下dbContext: public partial class EFContext : DbContext { public EFContext() : base("name=DbContext") { } public virtual DbSet<Users> Users { get; set; }
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");
}
}
外键分配不好,左连接重复,但我不知道如何修复它
关系处于用户状态:
HasOptional(u => u.Token).WithRequired(u => u.User);
注册表用户是1到0..1,用户令牌是可选的,PK/FK关系是UserID。实现的一种方法是从类令牌中删除UserID属性,并在令牌配置中配置关系,而不是调用映射键方法的UserConfiguration来显式指定外键
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");
}
}
它与我的代码配合得非常好。感谢您的帮助,我已经尝试过查看文档:。我获得以下错误:UserId:Name:类型中的每个属性名称都必须是唯一的。已定义属性名“UserId”。我认为这就是问题所在。EF对不同表上和虚拟机内部的用户ID越来越恼火property@CarlosLanderas您是否已从模型令牌中删除UserId的属性声明?我打赌你还没有搬走那处房产。这就是为什么你会犯这个错误。它现在正在工作。你是对的,我在Token类中注释了错误的属性。谢谢你,伙计。@CarlosLanderas很乐意帮助你
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");
}
}