Created
April 20, 2011 17:56
-
-
Save JasonOffutt/932139 to your computer and use it in GitHub Desktop.
Basic example of EF Code First w/ MVC...
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Thing | |
{ | |
[Key] | |
public int Id { get; set; } | |
public int ParentThingId { get; set; } | |
public virtual ParentThing Parent { get; set; } | |
} | |
public class ThingConfiguration : EntityTypeConfiguration<Thing> | |
{ | |
// Tells EF how to map the relationship... | |
public ThingConfiguration() | |
{ | |
this.HasRequired(t => t.Parent).WithMany().HasForeignKey(t => t.ParentThingId); | |
} | |
} | |
public class ParentThing | |
{ | |
[Key] | |
public int Id { get; set; } | |
public virtual ICollection<Thing> Things { get; set; } | |
} | |
public class ParentThingConfiguration : EntityTypeConfiguration<ParentThing> | |
{ | |
// Nothing needed here to map the relationship with Thing, | |
// but we need this class to add to the data context implementation | |
public ParentThingConfiguration() | |
{ | |
// More mappings to other objects as needed here... | |
} | |
} | |
public class ThingContext : DbContext | |
{ | |
public DbSet<Thing> Things { get; set; } | |
public DbSet<ParentThing> ParentThings { get; set; } | |
// DbModelBuilder is just ModelBuilder if you're not using the latest version of Code First | |
protected override void OnModelCreating(DbModelBuilder modelBuilder) | |
{ | |
modelBuilder.Configurations.Add(new ThingConfiguration()); | |
modelBuilder.Configurations.Add(new ParentThingConfiguration()); | |
} | |
} | |
public class ThingController : Controller | |
{ | |
private readonly IThingRepository repository; | |
public ThignController(IThingRepository repository) | |
{ | |
this.repository = repository; | |
// AutoMapper FTW | |
Mapper.CreateMap<ThingViewModel, Thing>(); | |
} | |
[HttpPost] | |
public ActionResult CreateThing(ThingViewModel model) | |
{ | |
if (ModelState.IsValid) | |
{ | |
var parentThing = repository.GetParentThingById(model.ParentThingId); | |
var thing = Mapper.Map<ThingViewModel, Thing>(model); | |
// Here comes the "magic". I'd really like to rip this out, but I needed it | |
// to get my tests passing. However, when I step through this and inspect the | |
// parentThing object, it never seems to be null when it's attached to | |
// the current ThingContext. It only ever seems to be null when I'm using | |
// a mocked repository. | |
if (parentThing.Things == null) | |
{ | |
parentThing.Things = new List<Thing>(); | |
} | |
parentThing.Things.Add(thing); | |
repository.Save(); | |
return RedirectToAction("w00t"); | |
} | |
TempData["Thing"] = model; | |
return RedirectToAction("TryAgain"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment