I am trying to get the attached program to work, but I get an error 26 cannot find server for the local db. This is a straight MSDN example. I can open the localdb with SSMS 2014, part of the sql express installation. Also in VS 2015 I can create a database connection to the localdb with (localdb)\\mssqllocaldb. The EF is version 6 (latest downloaded with NuGet).I have no idea why this is not working.
The code is:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.Entity;
namespace ConsoleApplication8
{
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; }
}
}
Maarten
use (localdb)\\\\mssqllocaldb
instead of (localdb)\\mssqllocaldb
. See \\\\
.
\\
is a escape character so you need to use two. I guess this is the problem.
did you add sql connection ?
using System.Data.SqlClient;
"user id=username;" +"password=pass.;server=servername;" + "Trusted_Connection=yes;" +"database=tablo_1; " + "connection timeout=30"
the servername should be like this : DESKTOP-I....\\\\SQL_2014;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.