How to extend and use EFMVC (codeplex.com)

I was looking for ways to extend and use EFMVC and the best way to learn is to just jump in!  I wanted to integrate the new “Simple Providers” with Entity Framework 5 in EFMVC and so I started looking into how to do that, which required me to learn how to extend EFMVC and how it is put together.  Here are the 6 steps to adding new functionality to EFMVC.

NOTE:  This is a work in progress. Please add your comments below so we can improve this.

 

Adding New Functionality

– new model, new db table…etc –

 

1       Add New Model

In EFMVC.Models project under the Entities folder, add a new class

  • Namespace must be namespace EFMVC.Model
  • Add DbSet for new model to inherited DbContext class
    • public DbSet<[New Model]> [New Model]s { get; set; }

 

2       Update SQL Server database from Model

In the PM console:

  • Add-Migration [*NAME OF UPDATE*] -ProjectName EFMVC.Data -StartUpProjectName EFMVC.Web -Verbose
  • Update-Database -ProjectName EFMVC.Data -StartUpProjectName EFMVC.Web -Verbose

3       Add Repository

In EFMVC.Data project under the Repositories folder, add a new class

  • Create two classes, one class from one interface
  • public class [New Model From Step 1]Repository : RepositoryBase<[New Model From Step 1]>, I[New Model From Step 1]Repository

public interface I[New Model From Step 1]Repository : IRepository<[New Model From Step 1]>

 

4       Create Configuration

In EFMVC.Data project under the Configurations folder, add a new class

  • public class [New Model From Step 1]Configuration : EntityTypeConfiguration<[New Model From Step 1]>
  • Register new configuration
    • In EFMVC.Data in EFMVCDataContex.cs under the OnModelCreating() function
      • modelBuilder.Configurations.Add(new [New Configuration From Step 4] ());

 

 

5       Create Command(s) for new model

In EFMVC.Domain project under the Commands folder, add new class(es)

  • Create a new folder with the name [New Model From Step 1]
  • Create a new file (class) in folder of type ICommand
    • Ex: CreateOrUpdate[New Model From Step 1]Command
      • Inherit from ICommand: public class CreateOrUpdateExpenseCommand : ICommand

 

6       Create Handler for new model

In EFMVC.Domain project under the Handlers folder, add new class(es)

  • Create a new folder with the name [New Model From Step 1]
  • Create a new file (class) in folder of type ICommand
    • Ex:    public class CreateOrUpdate[New Model From Step 1]Handler : ICommandHandler<CreateOrUpdate[New Model From Step 1]Command>
    • Create Constuctor (IoC) with appropriate repositories
      • Ex:  public CreateOrUpdateExpenseHandler(I[New Model From Step 1]Repository newRepository, IUnitOfWork unitOfWork)
  • Create function in new class
    • Ex:  public ICommandResult Execute([New Command From Step 5] command)

 

 

 

 

Next steps: Look into Universal Providers (to replace Simple Provider?)

Posted in asp.net, coding

Leave a Reply

Your email address will not be published. Required fields are marked *

*