using System; using System.Collections.Generic; using System.Linq; using System.Web; namespace repositoryPattern5.DAL { public class Repository : IRepository where TEntity : class { protected readonly RepoContext dbContext; public Repository(RepoContext _dbContext) { dbContext = _dbContext; } public void Add(TEntity entity) { dbContext.Set().Add(entity); } public TEntity Get(int Id) { return dbContext.Set().Find(Id); } public IEnumerable GetAll() { return dbContext.Set().ToList(); } public void Remove(int Id) { TEntity entity = dbContext.Set().Find(Id); dbContext.Set().Remove(entity); } } } using repositoryPattern5.Models; using System; using System.Collections.Generic; using System.Data.Entity; using System.Linq; using System.Web; namespace repositoryPattern5.DAL { public class RepoContext : DbContext { public RepoContext() : base("name=RepoContext"){} public DbSet products { get; set; } } } using repositoryPattern5.Models; using System; using System.Collections.Generic; using System.Linq; using System.Web; namespace repositoryPattern5.DAL { public class ProductRepository : Repository, IProductRepository { public ProductRepository(RepoContext dbContext) : base(dbContext) { } public IEnumerable GetTopProducts() { return dbContext.products.Take(2); } public RepoContext repoContext { get { return dbContext as RepoContext; } } } } using System; using System.Collections.Generic; using System.Linq; using System.Web; namespace repositoryPattern5.DAL { public interface IRepository where TEntity : class { IEnumerable GetAll(); TEntity Get(int Id); void Add(TEntity entity); void Remove(int Id); } } using System; using System.Collections.Generic; using System.Data.Entity.Core.Metadata.Edm; using System.Linq; using System.Web; namespace repositoryPattern5.DAL { public interface IUnitOfWork : IDisposable { IProductRepository products { get; } int Complete(); } public class UnitOfWork : IUnitOfWork { private IProductRepository _products; public IProductRepository products { get; private set; } private RepoContext _dbContext; public UnitOfWork(RepoContext dbContext) { _dbContext = dbContext; products = new ProductRepository(_dbContext); } public int Complete() { return _dbContext.SaveChanges(); } public void Dispose() { _dbContext.Dispose(); } } } using repositoryPattern5.Models; using System; using System.Collections.Generic; using System.Linq; using System.Web; namespace repositoryPattern5.DAL { public interface IProductRepository : IRepository { IEnumerable GetTopProducts(); } } using System; using System.Collections.Generic; using System.Linq; using System.Web; namespace repositoryPattern5.Models { public class Product { public int ProductId { get; set; } public string ProductName { get; set; } public string ProductType { get; set; } public string ProductDescription { get; set; } } }
Good...
how we can do it in database first approach? please share video if you can. Thanks
thanks but i need to tech me this u will try applay many times please please tech me this how i can doing
source code need
Source code pls
Source code please
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace repositoryPattern5.DAL
{
public class Repository : IRepository where TEntity : class
{
protected readonly RepoContext dbContext;
public Repository(RepoContext _dbContext)
{
dbContext = _dbContext;
}
public void Add(TEntity entity)
{
dbContext.Set().Add(entity);
}
public TEntity Get(int Id)
{
return dbContext.Set().Find(Id);
}
public IEnumerable GetAll()
{
return dbContext.Set().ToList();
}
public void Remove(int Id)
{
TEntity entity = dbContext.Set().Find(Id);
dbContext.Set().Remove(entity);
}
}
}
using repositoryPattern5.Models;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;
namespace repositoryPattern5.DAL
{
public class RepoContext : DbContext
{
public RepoContext() : base("name=RepoContext"){}
public DbSet products { get; set; }
}
}
using repositoryPattern5.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace repositoryPattern5.DAL
{
public class ProductRepository : Repository, IProductRepository
{
public ProductRepository(RepoContext dbContext) : base(dbContext) { }
public IEnumerable GetTopProducts()
{
return dbContext.products.Take(2);
}
public RepoContext repoContext
{
get
{ return dbContext as RepoContext; }
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace repositoryPattern5.DAL
{
public interface IRepository where TEntity : class
{
IEnumerable GetAll();
TEntity Get(int Id);
void Add(TEntity entity);
void Remove(int Id);
}
}
using System;
using System.Collections.Generic;
using System.Data.Entity.Core.Metadata.Edm;
using System.Linq;
using System.Web;
namespace repositoryPattern5.DAL
{
public interface IUnitOfWork : IDisposable
{
IProductRepository products { get; }
int Complete();
}
public class UnitOfWork : IUnitOfWork
{
private IProductRepository _products;
public IProductRepository products { get; private set; }
private RepoContext _dbContext;
public UnitOfWork(RepoContext dbContext)
{
_dbContext = dbContext;
products = new ProductRepository(_dbContext);
}
public int Complete()
{
return _dbContext.SaveChanges();
}
public void Dispose()
{
_dbContext.Dispose();
}
}
}
using repositoryPattern5.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace repositoryPattern5.DAL
{
public interface IProductRepository : IRepository
{
IEnumerable GetTopProducts();
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace repositoryPattern5.Models
{
public class Product
{
public int ProductId { get; set; }
public string ProductName { get; set; }
public string ProductType { get; set; }
public string ProductDescription { get; set; }
}
}