Ahmed Abd Ellatif
Ahmed Abd Ellatif

Reputation: 195

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?

Upvotes: 4

Views: 446

Answers (1)

Sampath
Sampath

Reputation: 65870

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);
    }
}

Upvotes: 4

Related Questions