C# 实体框架映射fluent api独立实体映射
我试图分离我的实体映射,但当我分离实体映射时,DB上没有应用任何更改。为什么? 以下是我的代码:C# 实体框架映射fluent api独立实体映射,c#,entity-framework,C#,Entity Framework,我试图分离我的实体映射,但当我分离实体映射时,DB上没有应用任何更改。为什么? 以下是我的代码: //class for example class UserMap : EntityTypeConfiguration<User> { public UserMap() { this.ToTable("User"); this.HasKey<int>(p => p.Id); this.Property(
//class for example
class UserMap : EntityTypeConfiguration<User>
{
public UserMap()
{
this.ToTable("User");
this.HasKey<int>(p => p.Id);
this.Property(u => u.UserPin).IsRequired().HasMaxLength(2000);
}
}
//my project context
public class ProjectDbContext : DbContext
{
public ProjectDbContext()
: base("name=DefaultConnectionString")
{
}
public DbSet<Project> Projects { get; set; }
public DbSet<Image> Images { get; set; }
public DbSet<User> Users { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
//inject my user map rules
base.OnModelCreating(modelBuilder);
modelBuilder.Configurations.Add(new UserMap());
}
}
如何注入我的地图配置以应用?您可以按如下所示进行尝试。您错过了公钥:D
public class UserMap : EntityTypeConfiguration<User>
{
public UserMap()
{
this.ToTable("User");
this.HasKey<int>(p => p.Id);
this.Property(u => u.UserPin).IsRequired().HasMaxLength(2000);
}
}
您所说的如何应用我的映射配置是什么意思?您已经使用modelBuilder.Configurations.Addnew UserMap;添加了配置;。您的意思是数据库不反映更改吗?如果是这样,您应该阅读有关迁移的内容。是的,我的意思是数据库不反映更改,并在包控制台中键入命令更新数据库,但不进行任何更改