vanilla T4 DAL template and poco generation - code-generation

I'm looking for T4 templates to generate completely vanilla poco and dal classes for a database table.
the poco should have a get, set for each column of the table. columns which aren't required should be represented with nullable types
The dal get method should use a datareader to return an IEnumerable

This video demonstrates an approach similar to what you are seeking: Julie Lerman's video on Using the POCO T4 Code Generation with the Entity Framework
Most people use the Entity Framework these days instead of datareaders. However, the T4 templates are customizable. You could modify the T4 DAL template to use a datareader instead of the Entity Framwork.

Related

What is entity framework designer? Why it is used in the Model-first approach?

I know entity framework designer is used to create the classes of model-first approach and then we create the database by using that class. But in code-first appraoch, it is possible to create the custom classes, within that classes the database is created automatically. Then what is the difference between code-first approach and model-first approach?
Code First is the more modern style of working with Entity Framework. As the name implies, you write the code first and the database model is generated for you, by using Entity Framework Migrations. In this scenario you are not using any graphical tool at all, everything is just pure code.
Model first means creating the abstract database model in the designer. The code is then generated by templates. If you update the model, the code will be regenerated.

Entity Framework 5 - POCO template Data Annotations

I downloaded EF5 which comes with T4 templates that create POCO classes. This works for me (database first) but there is no basic data annotations like [Required] or [MaxLength] that could easily come from EDMX.
My first quess is to edit the T4 template but I'm worried I'd have to update it with every new EF version update (when template changes), not to even mention that I'm sure T4 for these annotations was already coded.
Can anyone point me to the right direction, either to existing modified EF5 templates or other way to auto-generate "basic" data annotations from EDMX model? (and I don't mean Fluent API)
A little bit late, I know, but the answer is here:
T4 Metadata and Data Annotations Template
This T4 template handles generating metadata classes from an Entity
Framework 4 model and decorates properties with data annotation
attributes such as [Required] and [StringLength]. The [DataType]
attribute is also applied when appropriate. It'll also generate
ErrorMessage values based upon property names for required fields.

How does EF codefirst know which model class to create a table for?

I was not able to find the answer to this online - please link me if I've overlooked any resources.
I understand how Entity Framework's codefirst works. The question is: how does EF know which model class to create a table for and which model class to just treat as a class?
For example, in the sample MVC4 application that comes from creating a new MVC project with VS 2012 Express Developer, there are classes (LocalPasswordModel, LoginModel, RegisterModel, etc) in the Account Model that have no tables, and EF knows not to generate tables for these classes.
How does EF know this?
Entity Framework looks at your DbContext class, and creates a table for each DbSet<T> property that you define.
EF won't even see any class which is not referenced by the DbContext.

How to cleanly generate POCO classes from existing database using Entity Framework 4.3 Code First approach?

I'm following the EF Code-First approach in a project that works against an existing database, to which I'm adding tables as needed.
This database has a number of tables for which I need to generate POCO classes for, and so I was wondering if there was a straight-forward, clean approach, to generating simple POCO classes from the database ... from which I can continue to work with using the general Code-First paradigm?
You can use the Entity Framework Power Tools for that.
If you want just the simple Poco classes without any relations use this T4 template
Generate entity class from database table

Any Fluent API tutorials that use EF Database-First approach to explain the subject?

There are many tutorials on Fluent API, but they all explain it using Entity Framework Code-First code examples. Since I don't know Code-First, do you know of any Fluent API tutorials that would explain the subject using EF Database-First approach?
Thank you
There are no tutorials which would explain the Fluent API together with Database-First approach because Fluent API is made only for Code-First approach. You don't need the Fluent API if you want to create your model via Database-First.
Fluent API (together with Code-First data annotations and conventions) is a tool to define model details in code, such as string length, if a property is required or the type of relationship - many-to-many, one-to-many, etc. When using Database-First or Model-First the EDMX file has the same purpose - it contains all the details and mapping definitions of your model. Fluent API (+ data annotations and conventions) replaces the EDMX file only when using Code-First.
If you create the model via Database-First or Model-First, you will have an EDMX file representing your model. You can apply the T4 DbContext Generator to this EDMX file. The generated files have two characteristics being different from Code-First:
The generated connection string contains a section refering to the EDMX metadata which will be embedded into your assembly:
connectionString="metadata=res://*/Model.csdl
|res://*/Model.ssdl
|res://*/Model.msl;
..."
The generated context DbContext will have an overridden OnModelCreating method which just throws an exception:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
throw new UnintentionalCodeFirstException();
}
As long as you leave the metadata section in the connection string, EF won't even call OnModelCreating or any code in Fluent API in this method. The metadata section tells EF that your model is DB- or Model-First and that the metadata are defined in the embedded EDMX and not in Fluent API.
However, you can remove the metadata section from the connection string, remove the UnintentionalCodeFirstException and write code with Fluent API in OnModelCreating. You can follow this procedure to create an initial model via Database-First and then build on this initial model for further development with Code-First.
At this point you are not working anymore with Database-First, but Code-First instead and everything you read about Fluent API is valid for you.
There is an interesting post about accomplishing some of the “Database-First” objectives without employing the actual “Database-First” methodology per se.
http://agilenet.wordpress.com/2011/04/11/entity-framework-4-1-rc-with-an-existing-database/
The author uses “Code-First” and “Fluent API”, but disables the auto-generation and seeding of databases and tables.
He shares a sample that shows “how to create an entity model, then manually create your database and then map those entities to your database. Finally it shows using the DatabaseContext to save and retrieve entities”.
The part where he creates a “configuration class for each entity which maps between the entity and the database” is pretty cool. That step replaces the “edmx” files that would be generated when employing a formal “Database-First” approach.
This tutorial in a 6-part Database First tutorial series includes some fluent API examples:
http://www.asp.net/web-forms/tutorials/continuing-with-ef/using-the-entity-framework-and-the-objectdatasource-control-part-3-sorting-and-filtering