我正的错误是“无法打开数据库‘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,所以谢谢。
我不是100%确定,但尝试运行此管理,而不是使用localdb与您的当前用户,所以它不应该有权限问题。你也可以通过SSMS连接到(LocalDb)\ v11.0,这将让你看到实际的权限 –
嗯..如果我尝试连接到SSMS的localdb它给了我这个错误:“属性登录未设置” – ledgeJumper
你可以尝试重建你的本地数据库,这里概述http://social.msdn.microsoft.com/Forums/eu/LightSwitchDev11Beta/thread/484fb538-81eb-4eb6-b0bd-980dc85c5f7e –