Ahmed Abd Ellatif Ahmed Abd Ellatif - 3 months ago 23
C# Question

entity framework mapping fluent api seperate entity map

I'm trying to separate my entity map but when do that no changes applied on DB. Why?

Following is my code :

//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());

}
}


How to inject my map configuration to apply?

Answer

You can try as shown below.You have missed the public key word :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);
    }
}