In order to perform database CRUD operation from .net application,
the wrapper makes it quite easy by following the below steps.
In VS2015 or above, Create .net application [.net4.6.1] to interact with Sql server database
IDbContext type is inherited from the installed package
using Generic.Repository.EntityFramework; //add this reference
//DB is real data model created from database
public partial class DB : DbContext, IDbContext
{
public DB() : base("name=connectionstring"){}
IDbSet<T> IDbContext.Set<T>()
{
return base.Set<T>();
}
public DbSet<Customer> Customers {get;set;}
public int Save()
{
return base.SaveChanges();
}
....
}
- Now all set, please add the below lines from your consuming object/component
//add these 2 references
using Generic.Repository.EntityFramework;
using Unity;
public class ConsumeService
{
...
void ConsumeMethod()
{
var container = UnityConfig.Container; //get the Unity container
container.RegisterType<IDbContext, DB>(); // register your 'DataModel'
var dbManager=container.Resolve<IDBManager>(); //get the db manager
//All wired up. DO YOUR CRUD OPERATION & Customer is the table name in DB
var result=dbManager.GetRepository<Customer>().Get(); //GET All
//FIND by id, where id is the primary key
var result=dbManager.GetRepository<Customer>().Find(id);
// custObj is the Customer object
var result=dbManager.GetRepository<Customer>().Add(custObj); //ADD
var result=dbManager.GetRepository<Customer>().Update(custObj); // UPDATE
var result=dbManager.GetRepository<Customer>().Delete(custObj); // DELETE
}
App Name | Type | Comments |
---|---|---|
Generic.Repository.EntityFramework* | Class Library | EntityFramework wrapping business logic |
Generic.Repository.EntityFramework.Test | Test App | unit tests |
Generic.Repository.EntityFramework.ConsoleApp.Test | Console | Test the wrapping business logic |
*nuget package "Generic.Repository.EntityFramework" is a wrapper around ORM EntityFramework
Having any trouble? Check out our documentation or contact support and we’ll help you sort it out.