【问题标题】:Linq and localhost, entity namespace is different than program namespace but still get errorLinq 和 localhost,实体命名空间与程序命名空间不同,但仍然出现错误
【发布时间】:2018-06-19 04:17:57
【问题描述】:

我试图搜索这个,但我发现的每个示例都有一个问题,比如它们实际上与它们的类或其他东西具有相同的命名空间。

我只是想开始使用 Linq。当我添加新项目主机是本地主机。我在 Visualstudio 中有我的数据库,我的项目名称与 DataContext 名称不同,但我无法对其进行初始化。我得到错误:

'LinkedContext' 是一个命名空间,但用作类型'

这里是代码...

namespace TryAgain
{
    class Program
    {
        static void Main(string[] args)
        {
            LinkedContext db = new LinkedContext();
        }
    }
}

LinkedContext 不起作用?在数据库图的设置中,它说实体命名空间是“LinkedContext”所以我错过了什么。我想我看到您可以运行那一行代码来连接由于添加新项目而已经在 VisualStudio 中的数据库,然后开始使用它?我只想能够用数据库练习!执行以下操作:

var example = from x in example.Table
              orderby x.field
              select x;

【问题讨论】:

    标签: c# mysql entity-framework linq namespaces


    【解决方案1】:

    在代码顶部添加了“使用 LinkedContext”,然后还必须使用 LinkedDataContext 而不仅仅是 LinkedContext:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using LinkedContext;
    
    namespace TryAgain
    {
        class Program
        {
            static void Main(string[] args)
            {
                LinkedDataContext db = new LinkedDataContext();
    
                var example = from x in db.employees
                              orderby x.employee_id
                              select x;
    
    
                foreach (var whatever in example)
                {        
                    Console.WriteLine(whatever.name);
                }
    

    【讨论】:

      【解决方案2】:

      您的文件顶部需要using LinkedContext。你得到的错误是告诉你LinkedContext 是一个命名空间,但你把它当作一个类型,即一个类。一旦你在顶部定义了它,你就可以在该命名空间中使用你需要的类型。

      【讨论】:

      • 太棒了,这是朝着正确方向迈出的一大步。看起来它正在工作,但现在我只需要学习如何使用 Linq!我输入的是返回一些奇怪的东西。
      • 很高兴有帮助。现在您的输入是什么,您的预期输出是什么?
      • var example = from x in db.employees orderby x.employee_id select x;
      • 在 Console.WriteLine(example) 之后;我得到:从测试中选择 t1.FieldName、t1.FieldName2 等(测试是我的数据库的名称).employees t1 ORDER BY t1.employee_id
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-05-09
      • 2016-05-24
      • 2013-12-19
      • 2010-10-20
      • 2013-06-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多