Professional ASP.NET MVC 4
by Jon Galloway, Phil Haack, Brad Wilson, K. Scott Allen, Scott Hanselman
Data Access
The NuGet Gallery uses the “Code First” approach with Entity Framework 4.3.1 running against a SQL Server 2008 database. When you run the code locally, it runs against a SQL Server Express instance.
Code First is heavily convention-based and requires very little configuration by default. Of course, developers tend to be an opinionated lot with strong personal preferences and need to customize everything they touch, and the NuGet team is no different. There are a few conventions we replaced with our own configuration.
The EntitiesContext class contains our custom configuration for Entity Framework Code First. For example, the following snippet configures the property named Key as the primary key for the User type. If the property name had been Id, or if the KeyAttribute were applied to the property, this line would not be necessary.
modelBuilder.Entity<User>().HasKey(u => u.Key);
One exception to this convention is the WorkItem class, because that class comes from another library.
All the Code First entity classes are located in the Entities folder. Each entity implements a custom IEntity interface. The interface has a single property, Key.
The NuGet Gallery doesn't access the database directly from a DbContext derived class. Instead, all data is accessed via an IEntityRepository<T> interface.
public interface IEntityRepository<T> where T : class, IEntity, new() { void CommitChanges(); void DeleteOnCommit(T entity); T Get(int key); IQueryable<T> GetAll(); int InsertOnCommit(T ...Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access