4

I created Console Application in C#. I have installed Entity Framework by NuGet for this solution.

I have class Person:

  public class Person {

        public int Id { get; set; }
        public string LastName { get; set; }
        public string FirstName { get; set; }
        public DateTime BirthDate { get; set; }
    }

I have created db context:

  public class ApplicationDbContext : DbContext {

        public ApplicationDbContext()
        : base("MiniProjekt") { }
        public DbSet<Person> Persons { get; set; }
    }

Enabled migrations in Package Manager Console. The database connection just works. I get valid data for the code below(I inserted some objects earlier), but I don't know where database is.

    static void Main(string[] args) {
        var context = new ApplicationDbContext();
        Console.WriteLine(context.Persons.Count());
        foreach (Person person in context.Persons) {
            Console.WriteLine(person.LastName);
        }
        Console.ReadKey();
    }

The problem is that I don't know where is .mdf file with my database. I clicked Show All Files in Solution Explorer, went through my project files and I can't find it. I want to send this project later by mail and I would like to keep database file in the solution.

Question: How to import database file into my console application so it will be available by Solution Explorer(like in ASP.NET-MVC applicatons).

EDIT: Thanks to Frank I found the database but how to make it physically part of the Solution:

enter image description here

Yoda
  • 17,363
  • 67
  • 204
  • 344
  • 1
    Show your connection-string. – haim770 Dec 30 '14 at 16:22
  • @haim770 The funny thing that there is none. There is no web config etc. I just can't find this database. – Yoda Dec 30 '14 at 16:22
  • 1
    If you want to specify the location of the `.mdf` file, you'll have to create a connection string and specify the location (`AttachDBFilename=|DataDirectory|\db.mdf`). You can also create (or manipulate) a connection-string using C#, then pass it to the `DbContext` constructor. For a console application, you'll have to configurte `DataDirectory` explicitly, see http://stackoverflow.com/questions/1409358/ado-net-datadirectory-where-is-this-documented/1409378#1409378 – haim770 Dec 30 '14 at 16:28

1 Answers1

4

If you didn't configure a thing, your database will be in localdb. This is installed together with Visual Studio and you can find it here: Menu VIEW > SQL Server Object Explorer > Expand SQL Server > Expand all (localdb) nodes. Your database should be there.

The database is, don't ask me why, stored in your user folder. Right in your user folder. No sub directory. Look in c:\Users\YourUserName\, there should be some .mdf and .ldf files.

Edit:

Embedding the database in your command line project is not straightforward.

  1. Create an empty .mdf database or copy an existing one into your project. To create a new one, right click your project > Add > New Item... > Data > Service-based Database.

    You can put this in a subdirectory named Data if you want. Please notice that this file will be copied to your build directory whenever you build the application; you might set "Copy if newer" or "Never copy" in the properties to your liking.

  2. Add an application configuration file (app.config) to your command line project. Entity framework installation probably created one for you already.

  3. Add the connection string:

    <connectionStrings>
      <add name="YourContextClassName" connectionString="Server=(localdb)\ProjectsV12;Integrated Security=true;AttachDbFileName=|DataDirectory|YourDatabase.mdf;" providerName="System.Data.SqlClient" />
    </connectionStrings>
    

    YourContextClassName: The name of your DbContext.

    (localdb)\ProjectsV12: The instance name of the localdb SQL Server. This should match the instance name of your clients' localdb installation.

    YourDatabase.mdf: The name of your mdf file you created / copied in step 1.

  4. Add some code to define the DataDirectory variable:

    static YourContextClassName()
    {
        var baseDir = AppDomain.CurrentDomain.BaseDirectory;
        // You might Path.Combine(baseDir, "Data") here, if you want to have a
        // data subdirectory.
        var fullPath = Path.GetFullPath(baseDir);
        AppDomain.CurrentDomain.SetData("DataDirectory", fullPath);
    }
    

    This will tell our application what |DataDirectory| is in the connection string.

  5. Deploy LocalDB. Make sure that LocalDB somehow gets onto the machines of your clients. MSDN might help.

  6. Profit.

Frank
  • 4,461
  • 13
  • 28
  • 1
    Not necessarily LocalDB, it could be local SQL Express instance as well. – haim770 Dec 30 '14 at 16:24
  • Thank you I found it but how to make it part of the solution, by this I mean store it inside the solution directory, so when I send the project when I run the app it will look for database inside solution. Can I create Web.config or sth like that? – Yoda Dec 30 '14 at 16:28
  • @Yoda Sorry, I had a formatting problem. Look again. – Frank Dec 30 '14 at 17:09
  • Ok I get `Cannot attach the file 'C:\Users\s8359_000\Documents\Visual Studio 2013\Projects\Projekt5 — kopia\Projekt5\bin\Debug\Baza.mdf' as database 'Baza'.` So probably I should set path to `'C:\Users\s8359_000\Documents\Visual Studio 2013\Projects\Projekt5 — kopia\Projekt5\Baza.mdf` but I can't find how to get solution root directory. – Yoda Jan 01 '15 at 11:10