0

I am working with EF code first and create some classes whose data I like to save in local database added in my C# console application.

We can add localdb in asp.net project and database file is added to the app_data folder. In my console apps there is no option to add local db rather I saw there we can add service-based database which I added in my folder of project solution.

I do not know if "service-based database" is the same as "localdb" ?

I create a connection string for my EF to connect that database like this:

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

This is my small code with EF code first which suppose to connect my local database.

These are my domain classes and context

public class Blog
{
    public int BlogId { get; set; }
    public string Name { get; set; }
    public virtual List<Post> Posts { get; set; } 
}

public  class Post
{
    public int PostId { get; set; }
    public string Title { get; set; }
    public string Content { get; set; }

    public int BlogId { get; set; }
    public virtual Blog Blog { get; set; } 
}

public class BloggingContext : DbContext
{
    public BloggingContext()
        : base("name=BloggingContext")
    {
    }

    public DbSet<Blog> Blogs { get; set; }
    public DbSet<Post> Posts { get; set; } 
}

I am trying to add data using EF to my local database like this:

Console.Write("Enter a name for a new Blog: ");
var name = Console.ReadLine();

var blog = new Blog
        {
            Name = name,
            Posts = new List<Post> 
            { 
                new Post {Title="Desc2", Content="Test2"},
                new Post {Title="Desc3", Content="Test3"} 
            }
        };

db.Blogs.Add(blog);
db.SaveChanges();

When I am running this code, I get no error and code executes successfully, but when I open my localdb from database folder, then I didn't found any tables that had been created there. I know EF automatically creates tables.

I can understand that EF is not pointing to my database whose path is specified in connection string. I am not being able to understand what is wrong there for which EF not pointing to my local db added in database folder.

Here is my solution screen shot. All can see there is no table at left size.

enter image description here

Please help me in such way as a result EF should save the data in my database whose path I mention in my connection string. Thanks

Problem solved:

The problem was a wrong connection string. I remove the data directory from the connection string and specify full path of database file there and problem is solved.

My new connection string looks like

<add name="BloggingContext" 
     connectionString="Data Source=(LocalDb)\v11.0;AttachDbFilename=C:\Users\Mokasna\Documents\Visual Studio 2013\Projects\CodeFirstTest\CodeFirstTest\DB\Test.mdf;Integrated Security=True"
     providerName="System.Data.SqlClient" />
Monojit Sarkar
  • 2,353
  • 8
  • 43
  • 94

0 Answers0