I just jumped into a new project with a new company using Entity Framework and A
ID: 651495 • Letter: I
Question
I just jumped into a new project with a new company using Entity Framework and ASP.NET MVC 4. I am no expert on Entity Framework, but I think I have a decent grasp of how to use it.
From what I can tell, my models should reflect a table in the database omitting the columns I don't need. Also, we can use stored procedures with Entity Framework, or an IoC to map the model.
I need to connect the relationship with two objects, whose relationship is pretty nested. I could create a stored procedure to do this or create a bunch of models (based on the tables in the database) and use an IoC configuration to have a table from the database for each model, then query the models in the repository layer for the data I need using Linq to Entities. Seems like I would have a lot of ".Include(x=>x.SomeModel)"
My question is: which option is better for maintenance and integration in the future of the project? A stored procedure or Linq to entities?
Or am I not understanding Entity Framework properly?
Explanation / Answer
I think you've definitely got the gist of EF, that's a good question.
I would recommend that you go down the Linq to Entities option. Don't be afraid to use .Include(x => x.a), and remember that because this joins on another table you'll want to make sure your joins (foreign keys) have indexes on them. To me the maintenance benefit of the entity framework is being able to modify your table structure (add/move/rename columns) and then be able to re-compile your c# code and pickup what's broken - man, this would have saved me DAYS of fixing up stored procs a few years ago after making DB changes. If your logic is inside a stored proc then you don't get this benefit.
Only if your c# is starting to get too complicated to maintain would you consider using a stored proc to do the work, which might be better for maintenance purposes. Or if you're querying a database that can't be effectively used by the entity framework (compound foreign keys or something else).