MSDN实体框架代码第一个教程:SQL错误

时间:2014-01-31 21:00:52

标签: c# entity-framework ef-code-first sqlexception

我正在经历this tutorial on MSDN。 这是本教程第一部分中的所有代码。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.Entity;

namespace CodeFirstNewDatabaseSample
{
class Program
{
    static void Main(string[] args)
    {
        using (var db = new BloggingContext())
        {
            // Create and save a new Blog 
            Console.Write("Enter a name for a new Blog: ");
            var name = Console.ReadLine();

            var blog = new Blog { Name = name };
            db.Blogs.Add(blog);
            db.SaveChanges();

            // Display all Blogs from the database 
            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.WriteLine(item.Name);
            }

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

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 DbSet<Blog> Blogs { get; set; }
    public DbSet<Post> Posts { get; set; }
}
}

编译这个,根据教程应该给我这个:

输入新博客的名称:ADO.NET Blog
数据库中的所有博客:
ADO.NET博客
按任意键退出...

但我得到一个未处理的SqlException:
数据库'master'中的CREATE DATABASE权限被拒绝。

教程是否缺少关键信息或我的设置有问题?

编辑:所以我的同事说我在app.config中缺少connectionStrings。

添加后:

<connectionStrings>
    <add name="blogContext" connectionString="Server=(localdb)\v11.0;Integrated Security=true;" providerName="System.Data.SqlClient"/> 
</connectionStrings>

控制台应用程序运行,我再次运行程序时能够看到多个博客条目。但是教程中的下一步是通过服务器资源管理器进行连接,((localdb)\ v11.0)或(。\ SQLEXPRESS)都不显示为选项。

0 个答案:

没有答案