asp.net-mvc


EF - work with universal entity in library project, when not specified database entities


I would like to create library with base repository. But I solved problem that the library dont know what DbContext will be used. That will be created through the asp.net project. This is my code:
class BaseRepository<T> : IRepository<T> where T : class
{
private ExampleDbEntities _dbContext;
public void Add(T entity)
{
throw new NotImplementedException();
}
public void Update(T entity)
{
throw new NotImplementedException();
}
}
ExampleDbEntities is child of DbContext. I need replace it. BaseRepository dont know this db context. What I use instead? This BaseRepository will be inherited in asp.net project where is instance of DbContext.
Thank for advice
I think this is what you're looking for.
public abstract class GenericRepository<C, T> : IGenericRepository<T> where T : class where C : DbContext
{
private readonly C _entities;
public GenericRepository(C context)
{
_entities = context;
}
public virtual IQueryable<T> GetAll()
{
IQueryable<T> query = _entities.Set<T>();
return query;
}
public IQueryable<T> FindBy(System.Linq.Expressions.Expression<Func<T, bool>> predicate)
{
IQueryable<T> query = _entities.Set<T>().Where(predicate);
return query;
}
public virtual void Add(T entity)
{
_entities.Set<T>().Add(entity);
}
public virtual void Delete(T entity)
{
_entities.Set<T>().Remove(entity);
}
public virtual void Edit(T entity)
{
_entities.Entry(entity).State = EntityState.Modified;
}
public virtual Task SaveAsync()
{
return _entities.SaveChangesAsync();
}
}
public interface IGenericRepository<T> where T : class
{
IQueryable<T> GetAll();
IQueryable<T> FindBy(Expression<Func<T, bool>> predicate);
void Add(T entity);
void Delete(T entity);
void Edit(T entity);
Task SaveAsync();
}

Related Links

mvc 4 I've deployed to production server. Now cannot connect to localdb
Extra Paging Section at the Top of Kendo UI Grid using Razor
MVC 5- Application showing error message for System.Web.WebPages.Html.HtmlHelper for controls like viewbag
How to get custom date [only YYYY] validated MVC razor?
MVC Survey and capturing data
ReadOnly Form Properties
Add string prefix to automapped model property
MVC 4.0 Clearing output cache using HttpResponse.RemoveOutputCacheItem
How to disable open ASP.NET MVC site in IFrame?
Index ActionMethod in Home Controller would be called twice in ASP.Net MVC
how to determine if page is using http or https in a view?
Azure MVC4 RequireHttps not redirecting to https site
How can I store the Web Application Username in a DB UpdatedBy or CreatedBy column?
Expand IQueryable to use custom linq extension
MVC Controller to get image with ChildActionOnly
sitefinity how to send file to client

Categories

HOME
performance
jsp
angular2-forms
compliance
graylog2
backand
svg.js
intel-xdk
wikipedia-api
freebsd
akka.net
silverlight
calayer
pie-chart
asciidoc
librsvg
hyperion
tapi
eigen3
curve-fitting
git-bash
jscodeshift
revitpythonshell
multiprocessing
datamatrix
bitcode
italic
confidence-interval
intersystems
regex-group
nested-loops
angular-ngmodel
flatbuffers
json-c
toastr
spring-websocket
blockui
inria-spoon
haxm
advanced-installer
gridstack
paypal-rest-sdk
macvim
zipline
perl-module
runge-kutta
spring-saml
subscription
backwards-compatibility
wp-api
default-browser
ssms-2012
wrk
laravel-routing
complement
htc
jawr
itamae
hard-drive-failure
evaluate
olingo
building
facebook-audience-network
codeigniter-upload
android-location
securesocial
accounts
mysqladmin
linqtocsv
coldfusion-10
ospf
par
interpreted-language
cng
kindlegen
nbug
base-conversion
fortrabbit
min3d
bit.ly
jasmin
mklink
esx
short
hobbitmon
b2b
modalpopupextender
qdebug
jquery-data
couchpotato
rc-shell
rgba
exact-synergy-enterprise
nntp
renderaction
myisam
program-transformation
drupal-gmap
project-settings
non-relational-database
run-length-encoding
68hc11
newsgroup
graceful-degradation
user-testing

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App