views:

49

answers:

3

Quick question for everyone:

Do I need to include all the database table fields on my EF model?

For example; I've created a sub-model that only deals with tblPayment and associated tables. Now, I need to write a LINQ query to get some information about items. I would typically get this by joining tblPayment to tblInvoice to tblInvoiceItem to finally tblOrderItem.

I'm wondering if when I add in those other tables, do I need to include all the fields for tblInvoice and tblInvoiceItem? Ideally; I'd just like to keep the fields I'd need to join on, as that would limit the possibility of my sub-model breaking if other fields on those tables are modified/deleted.

Can I do this?

A: 

Yes, you can remove other fields from the entities.

SLaks
A: 

No, you don't need to include them all.

However, the GUI mapping tool, when reverse-engineering an existing DB into an EF model, will always include all columns, and there's no way to tell it not to.

Therefore, to exclude columns, you must do one of the following

  • Manually edit the EDMX yourself. Simply deleting the columns in the GUI designer may work, but only removes the columns from CSDL, not SSDL. The EF may or may not let you do that, depending upon the column's SSDL mapping.
  • Generate the model from a different DB, which has a similar schema except that it lacks those fields.
  • Code-first or model-first (EF 4 only).
Craig Stuntz
A: 

You can not only remove fields from entities, your entities can be combinations of different tables.

Entity Framework

JustLoren