Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EF Poco Generate for specific table

It is possible to generate a POCO class for one selected table in entity framework.

There are few tools that does that, but it does generate for all the table,

The primary reason i am asking this is to generate a POCO class for a single table, and add attributes to it manually, and the all i need to do is to attach it to DbContext as a property :).

As for example using Package-manager, they way it is done to add migration

generate-poco TableName FileName.cs
like image 309
Parimal Raj Avatar asked Aug 13 '26 19:08

Parimal Raj


1 Answers

You can use the ADO.Net wizard to generate the POCO classes for you from the database. The wizard will generate one partial class per table with properties that match your field names. Here is what I've done in the past when I wanted to add a new large table to an existing code first context.

  1. Add->New Item -> ADO.Net Entity Data Model
  2. Move the generated POCO classes to some other folder (and edit the namespace)
  3. Delete the rest of the extra stuff (I.e the .edmx file and generated context)
  4. Add the DBSet to 'your' DBContext

If you are using EF migrations

  1. add-migration
  2. update-database
like image 113
Phil Cazella Avatar answered Aug 15 '26 09:08

Phil Cazella



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!