Entity Framework custom configuration


With EF Code-First it’s easy create a database starting from domain objects,
but it’s always possible create an our custom configuration.

In my project I have two class, Role and User:

public class Role
{
    public Guid Id { get; set; }
    public string Name { get; set; }
}

public class User
{
     IList<Role> _role;

     public virtual IList<Role> Roles
     {
        get { return _role ?? ( _role = new List<Role>() ); }
     }

     public Guid Id { get; set; }

     public string UserName{ get; set; }
     public string FirstName { get; set; }
     public string LastName { get; set; }
}

If you want create custom configuration for User and Role, you can write others two class that works as configuration, RoleConfigration and UserConfiguration that extend EntityTypeConfiguration:

// custom configuration for Role
public class RoleConfiguration : EntityTypeConfiguration<Role>
{
     public RoleMapping()
     {
         ToTable( "Roles" ); // ef will create a table with name Roles
         HasKey(o => o.Id);  // define a key
         Property( o => o.Name ).IsRequired(); // this property is required on repository
     }
}

// custom configuration for User
public class UserConfiguration : EntityTypeConfiguration<User>
{
    public UserMapping()
    {
         ToTable( "Users" ); // ef will create a table with name Users
         
         HasKey(o => o.Id);  // define a key
         // the property is required on repository
         Property( o => o.UserName ).IsRequired(); 
         // this property is nullable and map on column with name firstname
         Property( o => o.FirstName ).HasColumnName("firstname");
         // this property is nullable and map on column with name lastname
         Property( o => o.LastName ).HasColumnName("lastname"); 

         // configure many-to-many navigation property between
         // User and Role
         HasMany( o => o.Roles ).WithMany();
    }
}

Now, for use our custom configuration, we will simply add RoleConfiguration and UserConfiguration to DbContext:

public class MyDbContext : DbContext
{
    public virtual IDbSet CreateSet()
         where TEntity : class
    {
        return Set();
    }

    protected override void OnModelCreating( DbModelBuilder modelBuilder )
    {
        modelBuilder.Configurations.Add( new RoleConfiguration() );
        modelBuilder.Configurations.Add( new UserConfiguration() );

        base.OnModelCreating( modelBuilder );
    }
}

Hope this helps,
Tony

About these ads

2 pensieri su “Entity Framework custom configuration

  1. I, My name is thomas, I am interested in you chess movment validator.
    It is very interesting, well written, and I think with your approbation, we gonna use it for one of our personnal project.
    By the way, two things seems to be missing, chess en passant and YOu should not move a piece if it make your king chess, see the following test :
    [TestMethod]
    public void MovePiece_ButKingEchec_Success()
    {
    var board = Board.GetNewBoard();

    board.SetPiece(ChessColor.White, ‘D’, 5);
    board.SetPiece(ChessColor.White, ‘D’, 4);
    board.SetPiece(ChessColor.Black, ‘D’, 6);

    var result = board.MovePiece(‘D’, 5, ‘A’, 2);

    Assert.IsFalse(result.IsSuccess, result.Description);
    }

    Good week end!

    Regards,
    Thomas

  2. Hi Thomas, I’m glad you like it. I wrote chess-movment-validator some time ago and now it’s stopped. Next days I’ll see your suggestions for improve the library. Please use the codes in your projects and if you want send me informations about them.

Rispondi

Inserisci i tuoi dati qui sotto o clicca su un'icona per effettuare l'accesso:

Logo WordPress.com

Stai commentando usando il tuo account WordPress.com. Chiudi sessione / Modifica )

Foto Twitter

Stai commentando usando il tuo account Twitter. Chiudi sessione / Modifica )

Foto di Facebook

Stai commentando usando il tuo account Facebook. Chiudi sessione / Modifica )

Google+ photo

Stai commentando usando il tuo account Google+. Chiudi sessione / Modifica )

Connessione a %s...