2012-06-20 86 views
2

我正的错误是“无法打开数据库‘NerdlyDB’由登录请求。登录失败。 登录失败,用户计算机名\用户名“。无法打开生成的数据库从EF代码第一

这是我的连接字符串:

<add name="NerdlyDB" connectionString="Data Source=(LocalDb)\v11.0; Integrated Security=SSPI; initial catalog= NerdlyDB" providerName="System.Data.SqlClient"/> 

这个数据库是生成使用EF代码第一个方法我是新来的这一个,所以我会告诉你我的情况下,这样做是不亮的地方:

在我的实体课程:

namespace NerdlyThings.Models 
{ 
public class Post 
{ 
    public int ID { get; set; } 
    public string Title { get; set; } 
    public string Description { get; set; } 
    public string Content { get; set; } 
    public DateTime Date { get; set; } 
    public string Tags { get; set; } 
} 
} 

的DbContext类

namespace NerdlyThings.Models 
{ 
public class NerdlyDB : DbContext 
{ 
    public DbSet<Post> Posts { get; set; } 
    public DbSet<Comment> Comments { get; set; } 
    public DbSet<Image> Images { get; set; } 
    public DbSet<Quote> Quotes { get; set; } 
} 
} 

我看到的错误是显而易见的认证问题,但我不知道在哪里使用代码首先,只能通过在SQL Server中建立一个数据库进行设置管理工作室。

*编辑***

好了,我不是我原来这样做在电脑上,但我有一些时间来杀死在工作,所以这种通过以下简单的给了别的去了指令here

我在MVC4互联网应用程序模板中的Visual Studio 2012 RC中做了这个。像梦一样工作,我只能假设我在其他计算机上出现了一些奇怪的配置问题,或者一路上出现了一些混乱的问题。反正这里是我做过什么:

类:

namespace CodeFirstBlog.Models 
{ 
public class Blog 
{ 
    public int Id { get; set; } 
    public string Title { get; set; } 
    public string BloggerName { get; set; } 
    public virtual ICollection<Post> Posts { get; set; } 
} 

public class Post 
{ 
    public int Id { get; set; } 
    public string Title { get; set; } 
    public DateTime DateCreated { get; set; } 
    public string Content { get; set; } 
    public int BlogId { get; set; } 
    public ICollection<Comment> Comments { get; set; } 
} 

public class Comment 
{ 
    public int Id { get; set; } 
    public DateTime DateCreated { get; set; } 
    public string Content { get; set; } 
    public int PostId { get; set; } 
    public Post Post { get; set; } 
} 
} 

的DbContext类:

namespace CodeFirstBlog.Models 
{ 
public class BlogContext : DbContext 
{ 
    public DbSet<Blog> Blogs { get; set; } 
    public DbSet<Post> Posts { get; set; } 
    public DbSet<Comment> Comments { get; set; } 
} 
} 

我设置这些再创造了像这样(只是有它产生我的选择我的背景下,控制器和类):

public class BlogController : Controller 
{ 
    private BlogContext db = new BlogContext(); 

    // 
    // GET: /Blog/ 

    public ActionResult Index() 
    { 
     return View(db.Blogs.ToList()); 
    } 

    // 
    // GET: /Blog/Details/5 

    public ActionResult Details(int id = 0) 
    { 
     Blog blog = db.Blogs.Find(id); 
     if (blog == null) 
     { 
      return HttpNotFound(); 
     } 
     return View(blog); 
    } 

    // 
    // GET: /Blog/Create 

    public ActionResult Create() 
    { 
     return View(); 
    } 

    // 
    // POST: /Blog/Create 

    [HttpPost] 
    public ActionResult Create(Blog blog) 
    { 
     if (ModelState.IsValid) 
     { 
      db.Blogs.Add(blog); 
      db.SaveChanges(); 
      return RedirectToAction("Index"); 
     } 

     return View(blog); 
    } 

    // 
    // GET: /Blog/Edit/5 

    public ActionResult Edit(int id = 0) 
    { 
     Blog blog = db.Blogs.Find(id); 
     if (blog == null) 
     { 
      return HttpNotFound(); 
     } 
     return View(blog); 
    } 

    // 
    // POST: /Blog/Edit/5 

    [HttpPost] 
    public ActionResult Edit(Blog blog) 
    { 
     if (ModelState.IsValid) 
     { 
      db.Entry(blog).State = EntityState.Modified; 
      db.SaveChanges(); 
      return RedirectToAction("Index"); 
     } 
     return View(blog); 
    } 

    // 
    // GET: /Blog/Delete/5 

    public ActionResult Delete(int id = 0) 
    { 
     Blog blog = db.Blogs.Find(id); 
     if (blog == null) 
     { 
      return HttpNotFound(); 
     } 
     return View(blog); 
    } 

    // 
    // POST: /Blog/Delete/5 

    [HttpPost, ActionName("Delete")] 
    public ActionResult DeleteConfirmed(int id) 
    { 
     Blog blog = db.Blogs.Find(id); 
     db.Blogs.Remove(blog); 
     db.SaveChanges(); 
     return RedirectToAction("Index"); 
    } 

    protected override void Dispose(bool disposing) 
    { 
     db.Dispose(); 
     base.Dispose(disposing); 
    } 
} 

然后,我只是运行该应用程序,使用生成的视图来创建一个新博客。很棒。数据库是在我的App_Data文件夹中生成的,我可以很好地访问它并查看生成的模式。所以问题解决了,也许?从这一点上,我可以使用你的建议答案来调整数据库设置和whatnot,所以谢谢。

+0

我不是100%确定,但尝试运行此管理,而不是使用localdb与您的当前用户,所以它不应该有权限问题。你也可以通过SSMS连接到(LocalDb)\ v11.0,这将让你看到实际的权限 –

+0

嗯..如果我尝试连接到SSMS的localdb它给了我这个错误:“属性登录未设置” – ledgeJumper

+0

你可以尝试重建你的本地数据库,这里概述http://social.msdn.microsoft.com/Forums/eu/LightSwitchDev11Beta/thread/484fb538-81eb-4eb6-b0bd-980dc85c5f7e –

回答

0

到目前为止我发现的最佳方法是创建我自己的IDatabaseInitializer来运行我需要创建索引和创建用户的命令。

public class MyContext : DbContext 
{ 
    static private Initializer DbInitializer; 
    static MyContext() 
    { 
     DbInitializer = new MyContext.Initializer(); 
     Database.SetInitializer<MyContext>(DbInitializer); 
    } 
} 
public class Initializer : IDatabaseInitializer<MyContext> 
{ 
    public void InitializeDatabase(MyContext context) 
    { 
     string ddl = "(Some SQL command to e.g. create an index or create a user)"; 
     context.Database.ExecuteSqlCommand(ddl); 
    } 
} 
+0

我绝对喜欢这个想法,我正在努力实现它。 – ledgeJumper

+0

在我的实际代码中,我有一个DDL命令的'List ',并在循环中执行它们中的每一个。使所有DDL保持在一个地方变得容易。 –

+0

检查我的相当长的编辑,看看这一切都为我摆脱。不知道是什么原因引起的问题,将不得不进行调查,但我肯定喜欢你的答案,在这里给我更好的控制生成数据库。谢谢! – ledgeJumper

1

您是否将您的DBContext类连接到某个连接字符串?我的猜测是缺少,因此导致问题。我平时见过的DBContext类的构造函数指定的连接字符串(见下面的例子):

public class NerdlyDB : DbContext 
{ 
    public NerdlyDB() : base("NerdlyDB") 
    { 
    } 
    ... 

这将查找与name="NerdlyDB"一个<connectionString>web.config/app.config。有关使用连接字符串设置EF的更多信息,请参见此link

我也推荐在你的代码中使用@Eric J.的方法。您可以在silk.codeplex.com(特别是MileageStats.Data.SqlCe项目)中看到他的建议(和EF代码首先是一般)的深入示例

在另一个注释中,您看起来像缺少实际的代码第一个模型设置。查看Silk项目中的MileageStatsDbContext.cs文件以查看如何执行此操作的示例。