locked
EF5 error 0040: Type User_Groups is not defined in namespace (Alias=Self) RRS feed

  • Question

  • I have two classes: User, Group they're many to many relationship.

    In User Class, it has a navigator property PrimaryGroup, and PrimaryGroupId is the foreign key of GroupId, One User has zero or one PrimaryGroup. But one user also can have zero or many other groups.

    And one group can have zero or many users.

    Here is my code, I figure out the problem is in UserMap class:

            this.HasRequired(u => u.PrimaryGroup).WithMany(g => g.Users).HasForeignKey(u => u.PrimaryGroupId);

    But I'm not sure how to handle it.

     public class User : Entity<int>
        {
            public string EId { get; set; }
            public string FirstName { get; set; }
            public string LastName { get; set; }
            public string FullName { get { return LastName + "." + FirstName; } }
            public string Password { get; set; }
            public virtual byte[] Photo { get; set; }
            public bool IsEnable { get; set; }
            public int? PrimaryGroupId { get; set; }
            public virtual Group PrimaryGroup { get; set; }
            public virtual ICollection<Group> Groups { get; set; }
    
            public override bool IsTransient()
            {
                return this.id == null;
            }
        }
    
        public class Group:Entity<int>
        {
            public string GroupName { get; set; }
            public virtual ICollection<User> Users { get; set; }
    
            public override bool IsTransient()
            {
                return this.id == null;
            }
        }
    
    public class UserMap : EntityTypeConfiguration<User>
        {
            public UserMap()
            {
                this.HasKey(u => u.Id)
                    .Property(u => u.Id)
    
                    .HasColumnName("UserId");
    
                this.HasMany(u => u.Groups)
                    .WithMany(g => g.Users)
                    .Map(
                    c =>
                    {
                        c.MapLeftKey("UserId");
                        c.MapRightKey("GroupId");
                        c.ToTable("UserGroup");
                    });
    
                this.HasRequired(u => u.PrimaryGroup).WithMany(g => g.Users).HasForeignKey(u => u.PrimaryGroupId);
                
    
                this.Property(u => u.EId).IsRequired().IsUnicode().HasMaxLength(50);
                this.Property(u => u.LastName).IsRequired().HasMaxLength(50);
                this.Property(u => u.FirstName).IsRequired().HasMaxLength(50);
                this.Property(u => u.IsEnable).IsRequired();
                this.Property(u => u.Password).IsOptional().HasMaxLength(64);
    
                this.Property(u => u.Photo).HasColumnType("image").IsOptional();
    
            }
        }
    
    public class GroupMap : EntityTypeConfiguration<Group>
        {
            public GroupMap()
            {
                this.HasKey(g => g.Id)
                    .Property(g => g.Id)
                    .HasColumnName("GroupId");
    
                //this.HasMany(g => g.Users)
                //    .WithMany(u => u.Groups)
                //    .Map(c => c.ToTable("GroupUsers"));
    
                this.Property(g => g.GroupName).IsUnicode();
    
                this.Property(g => g.GroupName).IsRequired().HasMaxLength(64);
    
    
            }
        }



    : )Ex

    • Moved by ThankfulHeart Monday, February 25, 2013 3:40 AM English Question
    • Moved by Jason Dot Wang Wednesday, February 27, 2013 2:14 AM This thread is about ADO.NET Entity Framework and LINQ to Entities
    Sunday, February 24, 2013 3:24 PM

All replies

  • Hi Ex.Shen,

      Welcome to MSDN Forum Support.

      We're doing research on this issue. It might take some time before we get back to you.

      Sincerely,

      Jason Wang




    Jason Wang [MSFT]
    MSDN Community Support | Feedback to us

    Tuesday, February 26, 2013 3:26 AM
  • And I enclose the Entity<> classs

        public abstract class Entity<TIdentity> : EntityBase
        {
            protected Entity();
    
            public virtual TIdentity Id { get; protected set; }
    
            public override bool IsTransient();
        }

    Another problem, when I initialize a Seed sample to database, I got object not referenced error. The error come from TIdentity of the base class Entity. 

    UnitOfWork class is derived from DataContext, Here is the Seed initialize code

    public class SampleData : DropCreateDatabaseAlways<UnitOfWork>
        {
            protected override void Seed(UnitOfWork context)
            {
    
                    var groups = new List<Group>
                    {
                        new Group {GroupName = "GP1"},
                        new Group {GroupName = "GP2"},
                        new Group {GroupName = "GP3"},
                        new Group {GroupName = "GP4"},
                        new Group {GroupName = "GP5"},
                        new Group {GroupName = "GP6"},
                        new Group {GroupName = "GP7"},
                        new Group {GroupName = "GP8"},
                        new Group {GroupName = "GP9"}
                    };
                    groups.ForEach(i => context.Groups.Add(i));
                    
    
                    context.SaveChanges();
    }}

    At last I have to change the User class and Group class not inherit the Entity<> base class. 


    : )Ex

    Tuesday, February 26, 2013 6:09 AM