模型 - 部分类和 Datacontext 类不通信
我创建了一个单表联系人数据库,其中只有 3 列(ID、姓名和电话)。然后,我使用 model 文件夹中的联系人表创建了 ContactsDataContext。最后,我仍然在模型文件夹中创建一个分部类(公共分部类 Contact)。
现在,当我写这篇文章时,
public partial class Contact
{
public string MyContact
{
get
{
string name = this.Name ?? String.Empty;
}
// ... Other lines omitted
}
}
出现以下错误:“'ContactsManager.Models.Contact' 不包含 'Name” 的定义,并且没有扩展方法 'Name >' 可以找到接受类型为“ContactsManager.Models.Contact”的第一个参数(您是否缺少 using 指令或程序集引用?)”
有问题吗???即使智能感知也没有显示我的 DataContext 类的属性。然而,我过去写过一些部分类,没有任何问题。
谢谢。
I've created a one table contact DB, which has only 3 columns (Id, Name, and Phone). I've then created the ContactsDataContext using my table Contacts in the model folder. Finally, I create a partial class still in the model folder (public partial class Contact).
now when I write this
public partial class Contact
{
public string MyContact
{
get
{
string name = this.Name ?? String.Empty;
}
// ... Other lines omitted
}
}
I get the following error :"'ContactsManager.Models.Contact' does not contain a definition for 'Name' and no extension method 'Name' accepting a first argument of type 'ContactsManager.Models.Contact' could be found (are you missing a using directive or an assembly reference?)"
Is something wrong??? Even the Intellisense in not showing the properties from my DataContext class. Yet, I've written some partial classes in the past with no problem.
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
两个部分的命名空间是否相同?
Are namespaces the same on the two partials?
克里斯·罗登,
是的,我已经解决了。事实上,我在几个月前开始学习 ASP.NET MVC 时就已经问过上述问题。我买了一本叫做《ASP.NET MVC - The Beer House/Nick Berardi/Wrox》的书。 是一本神书,但是对于初学者来说不推荐。一般来说,东西就这样被扔出去,不知道它们是从哪里来的。
响应来自应用分部类的定义。其中,分部类必须:
如果您错过了上述任何条件,那么您将处于麻烦,因为这些标准将用于将所有部分类合并为一个唯一的类。
就我而言,我创建了一个名为 ContactDB 的表。创建 datacontext 类后,我将 ContactDB 表拖放到 Linq2Sql 编辑器上。您必须知道,这会创建以下类:
partial 关键字允许我这样写:
在阅读了部分类的定义后,我发现我没有达到其中一个标准。我将另一个部分类称为“Contact”,这与 ContactDB 不同。这足以让我疯狂三天,直到我读到定义。此外,如果您使用正确的名称定义分部类,但将其放在不同的名称空间中,您也会遇到麻烦。
因此,如果上述答案对您不起作用(我不确切知道您的问题),请检查 分部类的定义。不要忘记阅读 ScottGu 关于 Linq2Sql 的系列。
希望有帮助。
Chris Roden,
Yes, I've resolve it. In fact, I've asked the above question many months ago when I started learning ASP.NET MVC. I bought a book called "ASP.NET MVC - The Beer House/Nick Berardi/Wrox." Is a god book, but it's not recommendable for beginners. Generaly, things are thrown like that without telling where they come from.
The response came from applying the definition of a partial class. Among others, partial classes must:
If you miss any of the above criteria, then you'll be in trouble because those criteria, ll be used to merge all the partial classes into a unique one.
In my case, I created a table called ContactDB. After I've created the datacontext class, I've dropped the ContactDB table on the Linq2Sql editor. As you must know, that creates the following class:
The partial keyword allow me to write this:
After reading the definition of partial classes, I found out that I failed one of the criteria. I called my other partial class "Contact" which's different from ContactDB. That was enough to make me go crazy for 3 days until I read the definition. Moreover, if you defines the partial class with the right name but you put it in a different namespace, you'll get in trouble as well.
So, if the above answer doesn't work for you (I don't know exactly your problem), check the definition of the partial class.Don't forget to read the ScottGu's series on Linq2Sql.
Hope it helps.