English 中文(简体)
Linq-to-SQL intellisense不知道datacontext对象中的表
原标题:Linq to SQL intellisense has no knowledge of tables within the datacontext object

我可以用我的datacontext对象成功地连接到数据库。我能够成功地读取和写入数据库,但我不能使用其他人似乎正在使用的语法。

例如,当我想要表中的数据时,我必须执行以下操作:

db = new UserDataDataContext(WebConfigurationManager.ConnectionStrings["UserData"].ConnectionString);
IQueryable Users = db.GetTable<User>();

我希望能够编写linq查询,比如我如何看待其他人这样做:

db = new UserDataDataContext(WebConfigurationManager.ConnectionStrings["UserData"].ConnectionString);
var query = from u in db.User
            where u.UserName == "Test"
            select u;

但intellisense不将User识别为数据库的属性,因此不会编译。Intellisense不显示任何看起来与数据库的表或实体相关的属性。

以下是我收到的错误消息:

 System.Data.Linq.DataContext  does not contain a definition for  User  and no extension method  User  accepting a first argument of type  System.Data.Linq.DataContext  could be found (are you missing a using directive or an assembly reference?)

以下是我正在做的事情的概要:

我使用了数据库设计器,拖动了我想要的表。

然后我将其保存为dbml文件。

然后它为我创建了一个扩展dataContext的新类,名为UserDataContext。然后,我实例化一个名为db的UserDataDataContext的新实例,并从Web.config传入我的连接字符串。

然后,我尝试编写一个linq查询,将表名引用为db对象的属性,但它无法识别它们。

与我读过的所有例子相比,我似乎找不出我做错了什么。有什么想法吗?

最佳回答

我确信您在某个地方有以下变量声明:

// variable is of type System.Data.Linq.DataContext
DataContext db;

将其更改为:

// variable is now of the appropriate subclass s type
UserDataDataContext db;

如果<code>db</code>是一个局部变量,并且您可以将初始化和声明内联在一起,那么最好使用隐式类型:

// db is implicitly of type UserDataDataContext
var db = new UserDataDataContext(WebConfigurationManager.ConnectionStrings["UserData"].ConnectionString);

C#是一种安全静态类型的语言。尽管您的引用所引用的对象在运行时确实具有您所期望的属性,但编译器不会让它编译,因为变量的类型中不存在这些属性。

问题回答

暂无回答




相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

难以执行 REGEXP_SUBSTR

I m 查询Oracle 10g。 我有两张表格(样本数据见下文)。 i m 试图提取一些领域

SQL Query Shortcuts

What are some cool SQL shorthands that you know of? For example, something I learned today is you can specify to group by an index: SELECT col1, col2 FROM table GROUP BY 2 This will group by col2

PHP array callback functions for cleaning output

I have an array of output from a database. I am wondering what the cleanest way to filter the values is example array Array ( [0] => Array ( [title] => title 1 ...

OracleParameter and DBNull.Value

we have a table in an Oracle Database which contains a column with the type Char(3 Byte). Now we use a parameterized sql to select some rows with a DBNull.Value and it doesn t work: OracleCommand ...

Running numbers in SQL

I have a SQL-statement like this: SELECT name FROM users WHERE deleted = 0; How can i create a result set with a running number in the first row? So the result would look like this: 1 Name_1 2 ...

How to get SQL queries for each user where env is production

I’m developing an application dedicated to generate statistical reports, I would like that user after saving their stat report they save sql queries too. To do that I wrote the following module: ...

热门标签