5

Basically I've created a class library project containing a data access repository for other projects to use. I've added the EF6 package and enabled migrations. My connection string in app.config looks the following:

  <connectionStrings>
    <add name="Pbn" connectionString="Data Source=(LocalDb)\v11.0;AttachDbFilename=|DataDirectory|\Pbn.mdf;Initial Catalog=LM.DataAccess;Integrated Security=True" providerName="System.Data.SqlClient" />
  </connectionStrings>

(The project name is LM.DataAccess).

When I run the update-database command I get the following error:

A file activation error occurred. The physical file name '\Pbn.mdf' may be incorrect. Diagnose and correct additional errors, and retry the operation. CREATE DATABASE failed. Some file names listed could not be created. Check related errors.

What could be the issue here?

JensOlsen112
  • 1,279
  • 3
  • 20
  • 26
  • Related question - http://stackoverflow.com/questions/18882560/entity-framework-code-first-update-database-fails-on-create-database – RBT Jun 04 '16 at 17:51

1 Answers1

7

This is the problem:

AttachDbFilename=|DataDirectory|\Pbn.mdf

That's being interpreted as \Pbn.mdf, and it doesn't exist. This is because you aren't setting the |DataDirectory| value, so it's blank. See here on how to set it.

Community
  • 1
  • 1
simon at rcl
  • 7,326
  • 1
  • 17
  • 24
  • I see. However where do I change the dataDirectory? I mean unlike a web project there aren't a global.asax file(where I usually would put this) in my class library. – JensOlsen112 Nov 12 '14 at 17:14
  • True, but there is a Program.cs in console and WinForm apps. WPF has App.xaml.cs, so there's something for everyone! – simon at rcl Nov 12 '14 at 17:17
  • Yes but mine is a classs library which doesn't have neither. I tried adding Program.cs, like a console app would have but it didn't get fired. By the way - sorry for my delayed answers but I'm trying everything you're saying :) – JensOlsen112 Nov 12 '14 at 17:52
  • You need to a) set it whatever project uses your library b) set it in your library sometime before the first time you use the connection string c) don't use |DataDirectory|! Use the proper path! – simon at rcl Nov 12 '14 at 18:01