ASP.NET MVC3 System.ComponentModel.DataAnnotations和Association

时间:2011-02-16 10:28:43

标签: c# asp.net asp.net-mvc-3 data-annotations

这部分代码工作正常

[Association(Storage = "profile", ThisKey = "UserId", OtherKey = "UserId")]
public Profile User {
   get { return this.profile.Entity; }
   set { this.profile.Entity = value; }
}

但如果我在这个课程中添加

System.ComponentModel.DataAnnotations 

然后,找不到协会。

问题出在哪里?

2 个答案:

答案 0 :(得分:1)

看起来你有两个冲突的命名空间。尝试将Association更改为System.Data.Linq.Mapping.Association,因此它看起来像:

[System.Data.Linq.Mapping.Association(Storage = "profile", ThisKey = "UserId", OtherKey = "UserId")]
public Profile User {
   get { return this.profile.Entity; }
   set { this.profile.Entity = value; }
}

答案 1 :(得分:1)

对奥斯汀答案的改进是使用using声明:

using L2SAssociation = System.Data.Linq.Mapping.Association;

[L2SAssociation(Storage = "profile", ThisKey = "UserId", OtherKey = "UserId")]
public Profile User
{
   get { return this.profile.Entity; }
   set { this.profile.Entity = value; }
}