huyrua / efprs Goto Github PK
View Code? Open in Web Editor NEWEntity Framework POCO, Repository and Specification Pattern
License: MIT License
Entity Framework POCO, Repository and Specification Pattern
License: MIT License
Hi. Thanks for sharing this.
I have a one problem. I'm using Table per Hierarchy (TPH).
For example we have a base class:
public abstract class Entity
{
public virtual int Id { get; set; }
public virtual bool IsTransient()
{
return Id == default(int);
}
}
And base class for several entitites:
public abstract class Event:Entity
{
[MaxLength(50)]
[Required]
public string Name { get; set; }
[Required]
public string Description { get; set; }
[Required]
[MaxLength(100)]
public string ShortDescription { get; set; }
[Required]
public DateTime PublishDate { get; set; }
public int Duration { get; set; }
}
public class Film:Event
{
public string Director { get; set; }
public string ActorList { get; set; }
public override string ToString()
{
return Name;
}
}
public class Concert:Event
{
public string Genre { get; set; }
public override string ToString()
{
return Name;
}
}
My context:
public class MyContext:DbContext
{
public MyContext():base(ConfigurationManager.ConnectionStrings["MyContext"].ConnectionString)
{
}
public DbSet<Event> Events { get; set; }
public virtual void Commit()
{
base.SaveChanges();
}
}
Next, create context and repository:
var context = new MyContext();
EventRepository repository = new EventRepository(context);
var films = repository.GetAll<Film>();
But I get exception: the sequence does not have elements. In this code:
string entitySetName = ((IObjectContextAdapter)DbContext).ObjectContext
.MetadataWorkspace
.GetEntityContainer(((IObjectContextAdapter)DbContext).ObjectContext.DefaultContainerName, DataSpace.CSpace)
.BaseEntitySets.First(bes => bes.ElementType.Name == typeof(TEntity).Name).Name;
Hi, do you have sample code using features in your project?
Thanks!
Any particular reason to not include a way specify properties to eager load? Just wondering if there was something wrong with that.. I'm thinking I rather isolate all EF-specifics including the .Includes() calls to the same assembly as my generic repo implementation assembly.
I added params to GetQuery<>() and its overloads:
Queryable<TEntity> GetQuery<TEntity>(params Expression<Func<TEntity, object>>[] pExpandPropertySelectors)
and before return the objectQuery:
if (pExpandPropertySelectors != null)
{
objectQuery = pExpandPropertySelectors.Aggregate
(objectQuery, (pCurrent, pInclude) => pCurrent.Include(pInclude) as ObjectQuery<TEntity>);
}
also modified the GetByKey method to make a call out to DbContext.Entry(entityObject).Reference/Collection.Load() ..
Could you please help me with the following issue.
I have a web application using EF5 Code First with your Repository Pattern.
But when using the WebDbContextStorage like this in the Application_Start()
DbContextManager.Init("Default");
DbContextManager.InitStorage(new WebDbContextStorage(this));
When the app pool is running in Integrated Mode i recieve the following error message.
Object reference not set to an instance of an object.
when running in classic mode it is working fine.
When switching back to SimpleContextStorage it is also working fine for a single user.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.