C# 是一个名称空间,但与';类型';
更新 我再次创建了解决方案C# 是一个名称空间,但与';类型';,c#,C#,更新 我再次创建了解决方案 File > New > Project > VisualC# > Console Application Name : teststudent Solution name : teststudent 然后,我在SolutionExplorer中点击teststudent Add > New Item > ADO.Net Entity Data Model > Generate from database 新连接: sa
File > New > Project > VisualC# > Console Application
Name : teststudent
Solution name : teststudent
然后,我在SolutionExplorer中点击teststudent
Add > New Item > ADO.Net Entity Data Model > Generate from database
新连接:
save entity connection settings in App.Config as :
teststudententities
您希望使用哪个版本的实体框架
6.0
然后我检查了所有的桌子
Model Namespace:
teststudentModel
更新结束
我正在尝试创建一个查询,以显示Visual Studio 2013中名为students的数据库表中的所有记录。
studentdb是我的数据库的名称。我已建立与SQL Server数据库的连接。我可以在解决方案资源管理器中查看模型中的表。当我执行时,我得到一个生成错误:
'studentdb' is a 'namespace' but is used like a 'type'
program.cs文件:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace studentdb
{
class Program
{
static void Main(string[] args)
{
Query1();
}
private static void Query1()
{
using (var context = new studentdb())
{
var students = from studentdetails in context.students select studentdetails;
Console.WriteLine(" Query All students");
foreach (var student in students)
{
Console.WriteLine("{0} {1}", student.studentid, student.surname);
}
Console.Write("Press enter to exit"); Console.ReadLine();
}
}
}
}
您的命名空间和其中一个类名相同
namespace studentdb
using (var context = new studentdb())
我想你的意思是var context=new studententies()或类似的东西,而不是studentdb()studentdb是同一名称空间的名称。所以你不能像studentdb()那样使用它。您可以仅为类创建对象,但不能为名称空间只需在第7行将名称空间重命名为其他名称
namespace anotherNamespace
{
对于
new studentdb()
使用它的完整限定名。我尝试过:namespace studentdb using(var context=new studententies())找不到类型或命名空间名称“studententies”(是否缺少using指令或程序集引用?)在创建edmx文件时,您是如何命名dbcontext的?在VisualStudio中搜索关键字上下文,然后您应该能够找到someclassname:dbcontext。这里someclassname是您的实体上下文名称,您应该使用TestStudentEntity()替换studentdb(),而不是new studentdb(),谢谢您这么做!它必须与连接设置中使用的名称相同。