1

I've always used the edmx file approach and just run "update from database". Now i am using Code first from existing database.

When the DBA changes the database schema, how do I update my dbcontext file and POCOs to match? Is that what migrations are for? Do I just delete everything int he models folder and re-create?

3
  • Personally I just create the classes manually, and edit existing ones if required. It's called 'code-first' because normally you'd make the code changes first, then update the DB accordingly. Commented Oct 9, 2017 at 18:22
  • Makes sense. I guess I am just used to the db first way. Commented Oct 9, 2017 at 18:25
  • So should i not do code first if I dont have control of the db? Commented Oct 9, 2017 at 18:37

1 Answer 1

1

"Code-First" has two different meanings in EF. It's both a Modeling workflow and a Mapping style. You can do Code-First Mapping with Database-First Modeling. You simply regenerate your entity types after changing the database, or change them manually to match the database change. This is what's called "Code-First From and Existing Database"

Sign up to request clarification or add additional context in comments.

4 Comments

"You simply regenerate your entity types after changing the database" How do I do this part? 1. Delete the dbcontext file, the auto-generated pocos then 2. Right click > add ado entity thingy >code first from existing, etc etc?
Yep. Or do that into a separate project and then move them to your main project.
Cool, thanks. Is it a bad idea to do code first if I dont have control of the DB? I am not allowed to make schema changes. I switched to code first because the edmx file always seemed so big and code first just seems "lighter". That is the only reason I switched.
Code first to an existing database is my favorite way to use EF. You're spot-on about the "lightness", and it's the only way to do Database-First Modeling in EF Core. Customizing the mapping between your objects and database is just so much easier with attributes/fluent/etc. And EDMX mapping complicated and IMO not worth learning.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.