0

I've been trying to get myself into some EntityFramework lately.

My Problem now is pretty much the same as in this question, but the solutions given there seem quite 'workaround' and dodgy.

I want changes saved to the database with dbContext.SaveChanges() to affect the actual database I created within the project and not one that is generated each time I run the Code. Since I will use this in a project at work I'd like to know if there is a smoother, cleaner way than the ones proposed in the linked question.

I tried to change data source in app.config from the original (LocalDB)\MSSQLLocalDB;attachdbfilename=|DataDirectory|\Database.mdf to the direct path (I guess that is not the way it works though).

9
  • Without knowing anything about how EF has been configured, there's not much to go on here. Commented Oct 17, 2016 at 18:58
  • If you want to use an actual database, then use an actual database. Commented Oct 17, 2016 at 19:03
  • Well I honestly just created a new Project, added an EF-Reference, made a new model, added database, generated Tables and added the code generating bits. So I don't really know what this configuration would be or where I would configure it. Would there be an option for what I am looking for in that? Commented Oct 17, 2016 at 19:04
  • @Amy , well I want the changes to affect the database file I added in the project. I don't want to use one on an actual server for reasons of offline presentation and 'portability' of the the solution. Commented Oct 17, 2016 at 19:10
  • Weren't there different initialization options in EF? It has been a while since I used it. You had db.CreateIfNotExists, DropCreateIfModelChanges etc right? Is that gone? Sounds like your project is setup to DropCreateAlways of some sort. Commented Oct 17, 2016 at 19:41

1 Answer 1

1

So I got it almost right with editing the app.config.

You have to edit attachdbfilename=|DataDirectory|\Database.mdf to Path\To\Project\Database.mdf. Because |DataDirectory| will make the connectionString point towards \bin\debug\Database.mdf, which is a copy of the database you created in your Project. As pointed out by this post.

Could have guessed that by myself I guess, but I'll blame it on me beeing tired.

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.