Note that there are some explanatory texts on larger screens.

plurals
  1. POCannot open generated database from EF Code first
    primarykey
    data
    text
    <p>The error I am getting is "Cannot open database "NerdlyDB" requested by the login. The login failed. Login failed for user 'ComputerName\User Name'. </p> <p>This is my connection string:</p> <pre><code>&lt;add name="NerdlyDB" connectionString="Data Source=(LocalDb)\v11.0; Integrated Security=SSPI; initial catalog= NerdlyDB" providerName="System.Data.SqlClient"/&gt; </code></pre> <p>This database was generates by using EF code first approach. I am new to this one, so I will show you what I did in case it is off somewhere:</p> <p>On of my Entity Classes:</p> <pre><code>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; } } } </code></pre> <p>DBContext Class</p> <pre><code>namespace NerdlyThings.Models { public class NerdlyDB : DbContext { public DbSet&lt;Post&gt; Posts { get; set; } public DbSet&lt;Comment&gt; Comments { get; set; } public DbSet&lt;Image&gt; Images { get; set; } public DbSet&lt;Quote&gt; Quotes { get; set; } } } </code></pre> <p>I see that the error is obvious an authentication issue, but I don't know where to set it using code first, only via setting up a db in sql server management studio.</p> <p><em><strong></em>*EDIT***</strong></p> <p>Ok, so I am not by the computer I originally did this on, but I had some time to kill at work so gave this another go by following the simple instructions <a href="http://msdn.microsoft.com/en-us/data/gg685467.aspx" rel="nofollow">here</a></p> <p>I did this in Visual Studio 2012 RC in an MVC4 internet application template. Works like a dream and I can only assume I have either some strange configuration issue on my other computer, or something got messed up along the way. Anyway here is what I did:</p> <p>Classes:</p> <pre><code>namespace CodeFirstBlog.Models { public class Blog { public int Id { get; set; } public string Title { get; set; } public string BloggerName { get; set; } public virtual ICollection&lt;Post&gt; 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&lt;Comment&gt; 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; } } } </code></pre> <p>DBContext Class:</p> <pre><code>namespace CodeFirstBlog.Models { public class BlogContext : DbContext { public DbSet&lt;Blog&gt; Blogs { get; set; } public DbSet&lt;Post&gt; Posts { get; set; } public DbSet&lt;Comment&gt; Comments { get; set; } } } </code></pre> <p>I set these up and then created a controller like so (Just had it generated my selecting my context and the class):</p> <pre><code>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); } } </code></pre> <p>Then I simply ran the application, used the generated views to create a new Blog. Works great. The database was generated in my App_Data folder, I can access it fine and see the generated schema. So problem solved, maybe? From this point I can use your suggested answers to tweak db settings and whatnot, so thank you.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload