我正在尝试使用linq的executequery功能直接查询数据库。普通的linq查询工作正常,但我正在尝试做一些与linq过于复杂的事情。根据MS,这需要我添加的System.Data.Linq命名空间。他们的文档没有说其他任何必要。
Intellisense突出显示了下面的ExecuteQuery,并说:“上下文不包含ExecuteQuery的定义。我单击此修复程序,它将生成一个代码块。
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Data.Linq; ///this is shaded out
using System.Linq;
using System.Linq.Expressions;
using System.Text;
//code
string s = "select id,name from dbo.table";
return db.ExecuteQuery<Page>(s).ToList();
**Edit - Context Class**
using System.Data.Entity.Infrastructure;
using WebSiteModel2019.Models;
using System.Data.Linq; //greyed out
using System.Collections.Generic;
using System;
namespace MySite.DAL
{
public class Context : DbContext
{
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
Database.SetInitializer<WebSiteModel2019Context>(null);
base.OnModelCreating(modelBuilder);
}
public Context(string x) : base(x)
{
}
public WebSiteModel2019Context(string x) : base(x)
{
}
public DbSet<EmaillHost> EmaillHosts { get; set; }
public DbSet<UserLogIn> UserLogIns { get; set; }
///.. other tables
**Functions added by Intellisence**
internal object ExecuteQuery<T>(Func<string> toString)
{
throw new NotImplementedException();
}
internal IEnumerable<T> ExecuteQuery<T>(string v)
{
throw new NotImplementedException();
}
}
public class WebSiteModel2019ContextFactory :
IDbContextFactory<WebSiteModel2019Context>
{
public WebSiteModel2019Context Create()
{
return new WebSiteModel2019.DAL.WebSiteModel2019Context("cnlocal");
}
}
}
我在MS文档或其他任何地方都没有看到这些代码块,因此我不确定它们是否确实必要或应该包含哪些代码。 拥有它们(无论如何都是底部)可以消除智能错误,但是代码会碰到此块,并且由于那里什么也没有停止。看来我需要在此添加一些东西。
有人可以帮忙吗? 谢谢