我在visual 2012中有一个控制台应用程序项目。我想先使用Entity Framework 6代码。当我运行我的项目时,它不会在我的SQL Server中创建任何数据库。我无法在我的SQL Server中首先通过代码创建数据库。我在构建时没有遇到任何异常。运行
下面是我的代码:
namespace CodeFirstNewDBeSample
{
class Program
{
static void Main(string[] args)
{
using (var db = new BlogContext())
{
//Console.Write("Enter a name for new Blog:");
//var name = Console.ReadLine();
//var blog = new Blog { Name = name };
//db.Blogs.Add(blog);
//db.SaveChanges();
}
}
}
public class Blog
{
public int BlogId { get; set; }
public string Name { get; set; }
public virtual IEnumerable<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 BlogContext : DbContext
{
public BlogContext() : base("name=BlogDBString") { }
DbSet<Blog> Blogs { get; set; }
DbSet<Post> Posts { get; set; }
}
}
配置文件:
<connectionStrings>
<add name="BlogDBString" connectionString="data source=MY-PC;initial catalog=BlogDB;integrated security=True;" providerName="System.Data.SqlClient" />
</connectionStrings>