C# 实体框架代码首先没有错误但不工作
为什么我的代码不起作用? 当我尝试运行控制台应用程序时,它总是在C# 实体框架代码首先没有错误但不工作,c#,asp.net,visual-studio-2010,entity-framework,C#,Asp.net,Visual Studio 2010,Entity Framework,为什么我的代码不起作用? 当我尝试运行控制台应用程序时,它总是在db.Blogs.Add(blog)上停止,并且永远不会继续 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data.Entity; namespace CodeFirstNewDatabaseSample { class Program { static void Mai
db.Blogs.Add(blog)
上停止,并且永远不会继续
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Entity;
namespace CodeFirstNewDatabaseSample
{
class Program
{
static void Main(string[] args)
{
using(var db = new BloggingContext())
{
Console.Write("Enter the name of new Blog:");
var name = Console.ReadLine();
var blog = new Blog { Name = name };
db.Blogs.Add(blog); //Stop working here but no error.
db.SaveChanges();
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.Write(item.Name);
}
Console.WriteLine("Press any key to exit...");
Console.ReadLine();
}
}
}
public class Blog
{
public int BlogId { get; set; }
public string Name { get; set; }
public virtual List<Post> Post { 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 BloggingContext()
: base("Data Source=(localdb)\v11.0;Initial Catalog=CodeFirstNewDatabaseSample.BloggingContext;Integrated Security=True")
{
}
public DbSet<Blog> Blogs { get; set; }
public DbSet<Post> Posts { get; set; }
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Data.Entity;
命名空间CodeFirstNewDatabaseSample
{
班级计划
{
静态void Main(字符串[]参数)
{
使用(var db=new BloggingContext())
{
Console.Write(“输入新博客的名称:”);
var name=Console.ReadLine();
var blog=newblog{Name=Name};
db.Blogs.Add(blog);//在此处停止工作,但没有错误。
db.SaveChanges();
var query=来自db.Blogs中的b
orderby b.Name
选择b;
WriteLine(“数据库中的所有博客:”);
foreach(查询中的var项)
{
Console.Write(item.Name);
}
Console.WriteLine(“按任意键退出…”);
Console.ReadLine();
}
}
}
公共类博客
{
public int BlogId{get;set;}
公共字符串名称{get;set;}
公共虚拟列表Post{get;set;}
}
公营职位
{
公共int PostId{get;set;}
公共字符串标题{get;set;}
公共字符串内容{get;set;}
public int BlogId{get;set;}
公共虚拟博客{get;set;}
}
公共类BloggingContext:DbContext
{
公共博客上下文()
:base(“数据源=(localdb)\v11.0;初始目录=CodeFirstNewDatabaseSample.BloggingContext;集成安全性=True”)
{
}
公共数据库集博客{get;set;}
公共DbSet Posts{get;set;}
}
}
当我进入ASP.NET博客并按enter键时,什么也没有发生。
它应该创建一个新的数据库,其中有两个表名为Blog
和Post
并显示消息(数据库中的所有博客:ASP.NET Blog
)
抱歉,英语不好,请帮助您的代码清晰明了,但您必须将连接字符串更改为以下格式
public class BloggingContext : DbContext
{
public BloggingContext()
: base(@"Data Source=(localdb)\v11.0;Initial Catalog=CodeFirstNewDatabaseSample.BloggingContext;Integrated Security=True")
{
}
public DbSet<Blog> Blogs { get; set; }
public DbSet<Post> Posts { get; set; }
}
公共类BloggingContext:DbContext
{
公共博客上下文()
:base(@“数据源=(localdb)\v11.0;初始目录=CodeFirstNewDatabaseSample.BloggingContext;集成安全性=True”)
{
}
公共数据库集博客{get;set;}
公共DbSet Posts{get;set;}
}
您错过了连接字符串开头的“@”符号以避免转义字符。@YuliamChandra是的,我这样做了,但仍然没有显示错误。抱歉,我错过了注释代码,它在
db.Blogs.Add(blog)上仍在运行吗代码>行还是立即关闭?如果仍在运行,请等待发生的事情。@YuliamChandra我等待10分钟仍不工作,您可以尝试我的代码。只是尝试了一下,使用不同的constr,在这里工作得很好,尝试按vs上的pause
,看看它在哪里waiting@YuliamChandra该死,这是工作,我按了暂停
然后继续,它在“ConnectionString”中显示一个警告,它不工作是因为它应该是\\。谢谢你的帮助!