0

With Code-First, I could use the NotMapped attribute in order to prevent EF from creating the DB column. Is there a way to do the opposite? i.e., using a DB-first approach, can I instruct EF not to create a specific column into a property of the generated partial class?

EDIT: EF 6.1

nitsuka
  • 1
  • 3
  • How are you exactly doing the Code First model? Which version of EF? – JotaBe Feb 04 '16 at 15:50
  • I am indeed using DB-first and want to keep EF from generating one property in the model. – nitsuka Feb 04 '16 at 16:08
  • I misrtoet my question. You can do DB first in different ways. How are you doing it? which is your EF version? Are you geenrating an edmx model or a code first model form the database? – JotaBe Feb 04 '16 at 16:13
  • create a VIEW and import the VIEW instead of the table – JamieD77 Feb 04 '16 at 16:33
  • Good one. I actually did work-around it by wrapping it in the view model but I'd still like to know if there is an equivalent to 'NotMapped' in DB-first. It doesn't look that way though. – nitsuka Feb 04 '16 at 16:50

1 Answers1

0

Import the tables into the Model. Select the table and then the column you don't want, right-click on it and select 'Delete from Model'. I think hitting the Del key works as well. With a column selected (or a Navigation property) the Properties window will also allow you to change the name of the generated Property to something different from the DB column name.

simon at rcl
  • 7,326
  • 1
  • 17
  • 24
  • Yeah, I tried both those things before posting. If I remove the column from the edmx diagram I get a compile error "Problem in mapping fragments...in table...It has no default value and is not nullable" and if I change the name (seen here http://stackoverflow.com/questions/6931014/is-it-possible-to-prevent-entityframework-4-from-overwriting-customized-properti) then my linq-to-sql code breaks on runtime. Any ideas? – nitsuka Feb 04 '16 at 16:23
  • Proceed until you get the 'mapping fragments' problem, then Clean the solution and rebuild. See if that works. I get the mapping fragment problem occasionally without deleting columns. – simon at rcl Feb 04 '16 at 16:53
  • Worth a try. Sadly, it didn't work as error 3023 "mapping fragments" won't quit. Thanks anyway, appreciate it. – nitsuka Feb 04 '16 at 17:00
  • Ah - hold on. Just reread this bit: ".It has no default value and is not nullable". Of course if this is true then the model is flawed as it can't insert a new row. Can you give the column a default value in the database? Then it can insert without having to specify a value. – simon at rcl Feb 04 '16 at 17:53
  • "if this is true then the model is flawed as it can't insert a new row." Obviously, the whole idea behind this is to implement the property in another partial class that is not auto-generated by EF. I haven't yet found a way of doing exactly this and your suggestion is just too big a compromise as the column is an AK. – nitsuka Feb 05 '16 at 07:36