0

I know that entity framework has a database first approach. Now the question is whether it can generate the DAL (data access layer) code (not the models) for me.

Alim Ul Gias
  • 5,141
  • 2
  • 24
  • 39
  • 2
    Could you please give more details on what you mean by saying generate DAL code. And also database first approach is one of the options. You can also have model first and code first by using the DbContext that you have mentioned. – Incognito Aug 09 '11 at 05:12
  • I meant the classes having save update delete methods for the database tables – Alim Ul Gias Aug 09 '11 at 05:28
  • 1
    It generates for you a class inherited from ObjectContext and you will have in it all the methods to manipulate data. – Incognito Aug 09 '11 at 05:33

2 Answers2

2

When using a Object Relational Mapper (ORM), you don't typically have CRUD code in the traditional sense. Rather, it abstracts those operations into more object oriented operations.

For example, you don't "insert", you add the model class to the table, then save changes. The ORM automatically generates the SQL needed to make the Object model match the data model.

So my point is, your question displays a basic lack of understanding of how ORM's work and how they relate to data models. You should probably do a little reading.

Erik Funkenbusch
  • 90,480
  • 27
  • 178
  • 274
1

I'm not sure what you mean specifically by "DAL code", as that's a rather ambiguous term. I would consider your Entity types part of the DAL.

When you use a model-first or database-first approach, the Entity Framework tools can auto-generate a context class from your model .edmx, which will inherit from ObjectContext. It's easy to customize the generated context class with T4 templates by finding one online that already generates from a .edmx, and modifying to your liking.

Code-first development uses the DbContext, which is not typically auto-generated. Please see this post on Scott Gu's blog for more details on this.

Rob
  • 5,013
  • 5
  • 37
  • 60
  • By DAL code i meant data access layer codes. Suppose i have designed my database and now i want to add it on my project. May be i will use the EF database first approach. Now the question is, can the EF generate the classes containing the save update delete methods for those database tables – Alim Ul Gias Aug 09 '11 at 05:27
  • 1
    If you use the model-first or database-first approach, it will generate the `ObjectContext` and related Entity type classes. The entity objects do not contain save/update/delete methods because the context manages entity objects and their relationships, and also contains `SaveChanges`; `Refresh`; and `DeleteObject`. – Rob Aug 09 '11 at 05:32
  • 1
    [read this](http://stackoverflow.com/questions/5446316/ef-4-1-code-first-vs-model-database-first), you will get clear idea about the difference between each approach – Damith Aug 09 '11 at 05:35
  • Well thanks for the information. But in model first approach i have to again design the whole database again. This may be feasible for a small project but consider that my database is huge (having more than 50 tables) and i think in this case using the database first approach is the best idea (as i have already designed my database ). Now will the database first approach will do the same thing as the model first approach does ?? – Alim Ul Gias Aug 09 '11 at 05:37
  • Yes it will. In fact, I meant database-first in the beginning, but confused the two. It *seems* from @DSW's link that model-first also does. (I have edited database-first into my answer and comment.) – Rob Aug 09 '11 at 05:39
  • @Alim If you already have your database then why not to go with database first approach. As I have said you will have a class inherited from ObjectContext with all the functionality you need. – Incognito Aug 09 '11 at 05:40
  • @Incognito and Rob Thanks guys for the reply. May be now I can start some coding :) – Alim Ul Gias Aug 09 '11 at 05:44