0

I'm trying to learn Entity Framework. I have a database but it's not complete yet so i will add fields to my database and my model in future. I can do that easily and Entity Framework updating the class code accordingly. The problem is, for example i add this line to the code:

[Required()]

So it becomes like this:

[Required()]
public string BranchName { get; set; }

After adding a new field to database and my model, this code gets generated again and parts i include disappears. This makes sense and there is a huge warning above the code saying Auto Generated but i need to deal with it somehow. I want to use validation codes but i want to add new fields to the database too (obviously).

I can manually add my new fields to classes but how can i disable auto code generation? Also i don't want to do this if there is an other way, because i will change my models very frequently and it will be hard if there are so many new fields.

Hope i'm clear enough, thanks.

user5273382
  • 75
  • 1
  • 7
  • What's your problem exactly?! – Sirwan Afifi Aug 30 '15 at 20:33
  • @SirwanAfifi Well, i create a model based on my database, i add some validation code to classes, after i change my model, Entity Framework regenerates the classes so my code disappears. I want to fix that. – user5273382 Aug 30 '15 at 20:48
  • You can use Entity Framework Code First Engineering. – Oluwafemi Aug 30 '15 at 20:50
  • @Oluwafemi I want to use the designer too, is there a way to disable auto code generation? – user5273382 Aug 30 '15 at 20:59
  • http://stackoverflow.com/questions/13074468/how-do-i-stop-entity-framework-from-generating-both-objectcontext-and-dbcontext this might help. – Oluwafemi Aug 30 '15 at 21:04
  • Could you show one of the generated classes? – Sam FarajpourGhamari Aug 30 '15 at 21:06
  • I can't change "Code Generation Strategy", it's disabled. I'm using VS 2015 and it seems you can't change it. So, how people add validation code to auto generated class? – user5273382 Aug 30 '15 at 21:23
  • you should use code first instead of database first, you seem to mix those two currently. for this, look at the database initializers www.entityframeworktutorial.net/code-first/database-initialization-strategy-in-code-first.aspx – DevilSuichiro Aug 30 '15 at 21:44
  • @DevilSuichiro I read many things about code first but it seems you can't use the entity designer with it. – user5273382 Aug 30 '15 at 22:20

1 Answers1

0

I don't know any ways of adding data annotations to auto-generated classes.

The override behavior is the problem with the designer approach (and not only I believe but can't recall any other issue now). Therefore EF7 will only support code first as it's a more clean and safe approach since your classes don't get overwritten.

So you will have to use code first for your needs.

If you still want to use the designer you could try as an alternative a tool that can show you a live view of the database (SQL Server Express I think it's called) that gives you the same view)

Mihai Bratulescu
  • 1,915
  • 3
  • 27
  • 43