none
Navigation Property null when I do context.Users.Create(). Is that correct? RRS feed

  • Question

  • This is my first time developing a Data Access Layer using Entity Framework Code First.

    I'm trying to represent a table User and another Table UserFriends. On table UserFriends I have two columns, UserId and FriendId. Its primary key are both columns, and both are foreign keys to User.UserId.

    This is what I did on Entity Framework to represent it.

    My User class:

    public class User
    {
        public int UserId { get; set; }
        public String Name { get; set; }
        public int Age { get; set; }
        public String City { get; set; }
        public String Country { get; set; }
        public String Email { get; set; }
        public String InterestIn { get; set; }
    
        public virtual ICollection<User> Friends { get; set; }
        public virtual ICollection<User> FromWhomIsFriend { get; set; }
    }

    My UserConfiguration class:

    public UserConfiguration()
    {
        Property(d => d.Name).IsRequired();
        Property(d => d.Age).IsRequired();
        Property(d => d.City).IsRequired();
        Property(d => d.Country).IsRequired();
        Property(d => d.Email).IsRequired();
        Property(d => d.InterestIn).IsRequired();
    
        HasMany(d => d.MessagesSent).WithRequired(l => l.SentByUser).WillCascadeOnDelete(false);
        HasMany(d => d.MessagesReceived).WithRequired(l => l.SentToUser).WillCascadeOnDelete(false);
    
        HasMany(d => d.Friends).
            WithMany(d => d.FromWhomIsFriend).
            Map(c =>
                {
                    c.ToTable("UserFriends");
                    c.MapLeftKey("UserId");
                    c.MapRightKey("FriendId");
                });
        HasMany(d => d.WantsToDo).
            WithMany(a => a.Users).
            Map(t =>
                {
                    t.ToTable("UserActivities");
                    t.MapLeftKey("UserId");
                    t.MapRightKey("ActivityId");
                });
    }

    And, using a console application I add some data to test Data Acess Layer:

    using (var context = new AdnLineContext())
    {
    
        var user1 = context.Users.Create();
        user1.Name = "User1";
        user1.Age = 25;
        user1.City = "City1";
        user1.Country = "Country1";
        user1.Email = "email_1@email.com";
        user1.InterestIn = "User1's interests";
    
        var user2 = context.Users.Create();
        user2.Name = "User2";
        user2.Age = 26;
        user2.City = "City2";
        user2.Country = "Country2";
        user2.Email = "email_2@email.com";
        user2.InterestIn = "User2's interests";
    
        var user3 = context.Users.Create();
        user3.Name = "User3";
        user3.Age = 27;
        user3.City = "City3";
        user3.Country = "Country3";
        user3.Email = "email_3@email.com";
        user3.InterestIn = "User3's interests";
    
        context.Users.Add(user1);
        context.Users.Add(user2);
        context.Users.Add(user3);
    
        user1.Friends.Add(user2);
        user3.Friends.Add(user1);
    
        context.SaveChanges();
    }

    But, when I do user1.Friends.Add(user2);, I get a null reference exception.

    My question is: Is that normal? Is it normal than Friends is null?

    Tuesday, July 30, 2013 8:53 PM

Answers

  • Hi VansFannel,

    From your description,I notice the issue you are experiencing is that navigation property "Friends" is null.Before using it,we should create an instance first as below.

                    var user1 = context.Users.Create();
                    user1.UserName = "User1";
                    user1.Age = 25;
                    user1.Friends = new List<User>();
                    var user2 = context.Users.Create();
                    user2.UserName = "User2";
                    user2.Age = 26;
                    user2.Friends = new List<User>();
                    var user3 = context.Users.Create();
                    user3.UserName = "User3";
                    user3.Age = 27;
                    user3.Friends = new List<User>();
                    context.Users.Add(user1);
                    context.Users.Add(user2);
                    context.Users.Add(user3);
                    user1.Friends.Add(user2);
                    user3.Friends.Add(user1);
                    context.SaveChanges();

    Best Regards.


    • Edited by hoshino suzumi Wednesday, July 31, 2013 6:08 AM edit
    • Marked as answer by VansFannel Wednesday, July 31, 2013 6:29 AM
    Wednesday, July 31, 2013 5:55 AM