0

I am working on a project which has a fairly complex database, and the code is stop-gap while another system overhaul is underway.

I am fixing up some legacy code, and need to perform work quickly and use work-arounds wherever possible.

There is an existing database and I am using EF6 Datbase First with much success.

However, I am discovering various cases (up to my 4th one now) where a particular column is not needed in my code, and EF is tripping up on type conversion issues.

I have found the fast solution (and preferred by my client) is to simply remove these columns from the EF model - which I have been doing manually after "update from database" - which I must do each time I fix a view or table, etc.

Is there a way I can somehow instruct EF "Update from Datbase" operation to ignore certain columns?

I have seen this for Code First - where OnModelCreating can be overridden to set an ignore property - or where a similar attribute is added to the existing property to inform EF when to ignore certain properties.

I had thought about using a partial class - defined in a .cs file in a subfolder where I could put some code to cause this to happen - however I can not annotate the property - as it would be defined twice - once in my partial, and one in the .cs generated by the T4 template.

I've been searching online for a technique to manage this, and I have not found anything.

For example:

Database has Table X, Column1, Column2, Column3.

Update from Database will create entity model for Table X, with all 3 columns.

After Update From Database, I can removed "Column2" reference, and all is well - there is no Column2 property on the model class, and my code simply never references any data in Column2.

What I am seeking is something I can put somewhere - in the database, the EF model, or partial class that will not be over-written each time I do an Update from Database.

Any suggestions?

qxotk
  • 2,384
  • 5
  • 24
  • 39
  • 3
    It's worth noting that these types of situations are ones that Code-First really excels at handling. – David L Jul 18 '16 at 21:00
  • @DavidL I am not sure I know what you mean. Are you suggesting I use Code First or are you stating the fact that this situation is just not covered by EF in the Database First scenario? – qxotk Jul 18 '16 at 21:03
  • 3
    If you remove properties from the class model in the edmx they won't be re-generated when you update the model, is my experience. – Gert Arnold Jul 18 '16 at 21:03
  • @GertArnold Is that so? Is there a particular place in the model that should contain the changes that EF will respect on subsequent update from database? Can you specify which place(s) if it matters? Must I do it in the diagram view of the edmx? I have been editing the xml (because the diagram is a bit unwieldy) – qxotk Jul 18 '16 at 21:05
  • 1
    I have always used the designer to remove properties from classes. I think EF only adds them when it discovers new fields. I.e. when new fields are added to the store model, they're also added as new properties to the conceptual model (the class model). Existing fields in the store model without a mapped property in the class model will not result in regenerated properties when the model is updated. I think you removed fields + properties (+ mapping) entirely by editing the edmx. – Gert Arnold Jul 18 '16 at 21:11
  • 1
    Per @GertArnold I looked further into VS 2015 Designer view, and discovered that navigating to the Model Browser, then to the Entity (table) and then to the Mapping Details - I could then see that the Value/Property (right) side of Mapping Details has a drop-down that shows as the first option "Delete" - selecting this drops the mapping, and this exclusion is preserved across "Update from Database" tool runs. – qxotk Jul 18 '16 at 21:36
  • 1
    @Eris I agree, so I voted to close this as duplicate. If reasonable, I might seek to improve the original so that it might show up in a searching using the terms that I used when trying to find a solution. – qxotk Jul 18 '16 at 21:38
  • FYI I retracted my vote to close, because the duplicate I chose is not as good as the one from @GertArnold - and I cannot vote to close again. Hopefully enough votes will come from elsewhere. – qxotk Jul 18 '16 at 22:26
  • In 2023 with VS 2022 with Mapping Details, changing a column to Deleted causes an "Error 11009: Property 'x' is not mapped" on the next update from database – bla9x Aug 02 '23 at 01:59

0 Answers0