ASP.NET MVC 3:为什么它认为我的类是一个复杂的类型?

时间:2012-04-29 20:20:02

标签: c# asp.net-mvc-3 entity-framework ef-code-first

使用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的原因,我将不胜感激。提前谢谢!

ETA:来自我的模型的相关位:

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的内部工作原理。我在这里看到可能有问题的唯一事情是UserList<FriendGroup>FriendGroupList<User>。但是,注释FriendGroupComplexType的任何内容都不存在。

ETA(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确实有ListComplexType个带注释的对象,但EF没有抱怨这些。

namespace Clade.Models
{
    [ComplexType]
    public class AchievementEarned
    {
        public Achievement achievement { get; set; }
        [Timestamp]
        public DateTime dateEarned { get; set; }
    }
}

ETA(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;
    }

1 个答案:

答案 0 :(得分:0)

您可以尝试以下方法: 使用int属性而不是Enum 或者尝试使用更新到EF 5 beta 2 PM&GT; Install-Package EntityFramework -Pre 更好地支持枚举