简体   繁体   中英

How to import database files into solution for C# console application which uses Entity Framework

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:

在此输入图像描述

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM