c# - ASP.NET MVC 3 : why does it think my class is a complex type?

标签 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 的任何见解,我将不胜感激。 .提前致谢!

预计到达时间:我模型中的相关位:

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/

相关文章:

c# - .Net 奇怪的 session 错误 - 由于新 session 在回发期间变量消失

直到刷新页面后才应用 JQUERY Css

c# - 获取 Mvc3 Ajax.ActionLink 用 Jquery 替换数据?

entity-framework - ObjectContext 实例已被释放,不能再用于需要连接的操作

c# - 访问WPF DLL的ViewModel

C# - XML反序列化与二进制反序列化与二进制+Deflate

c# - .NET 编译器找不到引用

c# - Javascript window.location 在 MVC3 中不起作用

c# - 为什么 EF 在表中插入重复项?

c# - 使用 EF5 代码优先迁移执行 IDENTITY_INSERT