recreate a local database

steven.noppe

New member
Joined
Oct 11, 2021
Messages
4
Programming Experience
10+
I have a Visual C# .net application hosted on a git.
When I recently moved to a new laptop and re-installed visual studio, I cloned the repository succesfully, but i can not publish / run the application anymore because it's complaining about a lost "database.mdf" file.

error MSB3113: Could not find file 'AppData\Database.mdf'
error MSB3113: Could not find file 'AppData\Database_log.ldf

On my old laptop the location of the database was in the "AppData" directory. Offcourse it's not there anymore.

But my thought was that the project would re-create the database and make a whole new one.

Is this still possible and how do I do this?
 
If the project was using Entity Framework and following the code-first approach it should re-create the database for you at runtime. It doesn't sound like the case though if publishing is failing due to those failures at build time.
 
Do you have backups of your old laptop? Perhaps you have a copy of those files there.
 
If the project was using Entity Framework and following the code-first approach it should re-create the database for you at runtime. It doesn't sound like the case though if publishing is failing due to those failures at build time.

Can you explain this a little bit more please?
Maybe I can still change my project to follow this approach


and yes I have backup of those 2 files, but I don't think just copying these files are the correct sollution right now.
 
Show us your connection string, minus any sensitive data, of course.

Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\AppData\Database.mdf;Integrated Security=True;Connect Timeout=30

What I did now was copied the database file on my old laptop into my project folder and added the \AppData\ directory to the git repository.

Not the ideal solution but works for now
 
Odd setup. There should have been a question when you added/created database to add it to project and update connection string. If you answered 'yes' the database is added to project as Content file (+Copy to output directory mode), and connection string AttachDbFilename=|DataDirectory|\Database.mdf, notice no "\AppData\". |DataDirectory| substitudes as exe folder during debugging and a folder in AppData if published with clickonce. In publish settings, Application Files, the database files should be set as "Data File".
 
Back
Top Bottom