使用 Code First EF 构建我的项目。
我有一个 User
具有作为其属性之一的类 List<FriendGroup>
(其中 FriendGroup
基本上只是 User
的集合,有点像 Google+ 中的“圈子”)。 FriendGroup
在不同的文件中定义为 POCO 并且......这就是事情......我从来没有在任何地方说过它是 ComplexType
.
但是当我尝试运行我的应用程序时,我得到了异常,
System.InvalidOperationException: The type 'FriendGroup' has already been configured as an entity type. It cannot be reconfigured as a complex type.
对于 ASP.NET 为什么决定我的类是 ComplexType
的任何见解,我将不胜感激。 .提前致谢!
预计到达时间:我模型中的相关位:
namespace Clade.Models
{
public class User
{
[Key]
public int userID { get; private set; }
[Required]
public Profile profile { get; set; }
[Required]
public string userName { get; set; }
...
public List<FriendGroup> friendGroups { get; set; }
...
public List<AchievementEarned> achievementsEarned { get; set; }
}
}
namespace Clade.Models
{
public class FriendGroup
{
[Key]
[HiddenInput(DisplayValue = false)]
public int friendGroupID { get; private set; }
[HiddenInput(DisplayValue = false)]
public int userID { get; set; }
[Required]
public string friendGroupName { get; set; }
public Privacy defaultPrivacy { get; set; }
[Timestamp]
public DateTime dateCreated { get; set; }
public List<User> usersInFG { get; set; }
public void UpdateMe(FriendGroup editedFG)
{
friendGroupName = editedFG.friendGroupName;
defaultPrivacy = editedFG.defaultPrivacy;
usersInFG = editedFG.usersInFG;
}
}
}
还有 EF 代码、存储库等,但它们都不知道任何 POCO 的内部工作原理。我在这里看到的唯一可能有问题的是 User
有一个 List<FriendGroup>
和 FriendGroup
有一个 List<User>
.但是从来没有任何东西可以注释 FriendGroup
作为 ComplexType
.
预计到达时间 (2):Profile
也只是一个 POCO:
namespace Clade.Models
{
public class Profile
{
[Key]
public int profileID { get; private set; }
public User user { get; set; }
public DiscussionGroup dg { get; set; }
public string location { get; set; }
public Privacy locationPrivacy { get; set; }
public string aboutMe { get; set; }
public Privacy aboutMePrivacy { get; set; }
...
}
}
User
确实有 List
一对夫妇的 ComplexType
- 带注释的对象,但 EF 没有提示这些。
namespace Clade.Models
{
[ComplexType]
public class AchievementEarned
{
public Achievement achievement { get; set; }
[Timestamp]
public DateTime dateEarned { get; set; }
}
}
预计到达时间 (3):这是 UserRepository
中的方法错误发生的地方。它发生在以 var results
开头的行上.
public bool Add(User newUser)
{
bool rv = false;
//check to make sure no one else has the same username first
var results = from user in Users
where user.userName.Equals(newUser.userName)
select user;
if (results.Count() == 0)
{
this.context.Users.Add(newUser);
this.Commit();
rv = true;
}
return rv;
}
最佳答案
您可以尝试以下方法: 使用 int 属性而不是 Enum 或者尝试更新到 EF 5 beta 2 PM> 安装包 EntityFramework -Pre 更好地支持枚举
关于c# - ASP.NET MVC 3 : why does it think my class is a complex type?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10375645/