【发布时间】:2010-12-28 01:12:47
【问题描述】:
我有以下型号和配置:
public class User
{
public int Id { get; set; }
public User CreatedBy { get; set; }
public DateTime? DateCreated { get; set; }
public string Name { get; set; }
public string Username { get; set; }
}
public class UserConfiguration : EntityTypeConfiguration<User>
{
public UserConfiguration()
{
this.HasKey(t => t.Id);
this.HasOptional(u => u.CreatedBy);
this.Property(u => u.DateCreated);
this.Property(u => u.Name).HasMaxLength(64);
this.Property(u => u.Username).HasMaxLength(64);
}
}
public class SampleContext : DbContext
{
public IDbSet<User> Users { get; set; }
public SampleContext(string dbConnection)
: base(dbConnection)
{
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Configurations.Add(new UserConfiguration());
}
}
我可以很好地处理上下文并添加用户。然后,我查看了 Code First 生成的表,并意识到 CreatedBy 属性不是生成 CreatedById 列,而是生成 UserId 列。如果我添加另一个属性,例如ModifiedBy,它将生成UserId1 等等:
我也尝试使用HasRequired() 进行映射,但该属性仍会生成与实际类型匹配的列名称,而不是属性名称。任何帮助将不胜感激。
【问题讨论】:
标签: c# entity-framework code-first