Q:

Entity framework code first no errors but not working

Why my codes is not working?
When I try to run the console application it's always stop on db.Blogs.Add(blog) and never continue.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Entity;

namespace CodeFirstNewDatabaseSample
{
class Program
{
    static void Main(string[] args)
    {
        using(var db = new BloggingContext())
        {
            Console.Write("Enter the name of new Blog:");
            var name = Console.ReadLine();

            var blog = new Blog { Name = name };
            db.Blogs.Add(blog); //Stop working here but no error.
            db.SaveChanges();

            var query = from b in db.Blogs
                        orderby b.Name
                        select b;

            Console.WriteLine("All blogs in the database:");
            foreach (var item in query)
            {
                Console.Write(item.Name);
            }

            Console.WriteLine("Press any key to exit...");
            Console.ReadLine();
        }
    }
}

public class Blog
{
    public int BlogId { get; set; }
    public string Name { get; set; }

    public virtual List<Post> Post { 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("Data Source=(localdb)\v11.0;Initial Catalog=CodeFirstNewDatabaseSample.BloggingContext;Integrated Security=True")
    {
    }
    public DbSet<Blog> Blogs { get; set; }
    public DbSet<Post> Posts { get; set; }
}
}

When I enter ASP.NET Blog and pressed enter there's nothing happen.
It should create a new database with 2 tables named Blog and Post
and show the message (All blogs in the database: ASP.NET Blog)
Sorry for bad English, Please help 

A:

Your code is clear and straightforward but you have to change the connection string to the following format
public class BloggingContext : DbContext
{
    public BloggingContext()
        : base(@"Data Source=(localdb)\v11.0;Initial Catalog=CodeFirstNewDatabaseSample.BloggingContext;Integrated Security=True")
    {
    }
    public DbSet<Blog> Blogs { get; set; }
    public DbSet<Post> Posts { get; set; }
}

you miss the "@" sign in the beginning of the connection string to avoid the escape characters.

