0

I am doing a project for my course work, I am using database first entity framework and got my DBContext class as

public partial class StudentDBEntities : DbContext
    {
         public DbSet<LoginDetail> LoginDetails { get; set; }
        public DbSet<StudentDetail> StudentDetails { get; set; }
        public DbSet<UserFriend> UserFriends { get; set; }
}

Now I created a model object as below to use this model to pass on view.

public class ViewStudentDetail
    {    
        public StudentDetail Student { get; set; }
        public UserFriend Friends { get; set; }

    }

here is my StudentDetail class created default

public partial class StudentDetail
    {
        public StudentDetail()
        {
            this.UserFriends = new HashSet<UserFriend>();
        }
        public string StudentName { get; set; }
        public string UnivName { get; set; }
        public string City { get; set; }
        public string Username { get; set; }
        public string Password { get; set; }
        public string EmailId { get; set; }

        public virtual ICollection<UserFriend> UserFriends { get; set; }
    }

In my View I have find a friend scenario and text box value passed is ViewStudentDetail.Friends.username.to the post method

My problem is that I want to assign the StudentDetails with the given username from DbContext object (that are already saved in database during registration) to the model object In My controller i have like this

  StudentDBEntities DbAccess = new StudentDBEntities();
 ViewStudentDetail Studentdetails = new ViewStudentDetail();
    Studentdetails.Student = DbAccess.StudentDetails .Find(username);

This assignment is not working and is assigning null. Could you please tell me how to fetch the values in db to model object. Also if i need to assign each attribute to model please help me with the syntax to fetch from db object.

michaeld
  • 569
  • 1
  • 6
  • 10

1 Answers1

0

The DbSet.Find() method searches only for PRIMARY KEY matches, it will not search all/any properties on an entity.

I think you are looking for something like this:

ViewStudentDetail.Student = DbAccess.StudentDetails.FirstOrDefault(student => student.Username == username);

If not, please provide more of your source code in your question. Specifically, please include the generated entity classes.

kingdango
  • 3,979
  • 2
  • 26
  • 43
  • hi kingdango,I have edited and added . please check if that is ok to get me an answer. – michaeld Apr 17 '13 at 00:28
  • Please include the StudentDetail class. If you are using Database First then this would have been generated for you. Have you tried my suggestion to use .FirstOrDefault instead of .Find ? – kingdango Apr 17 '13 at 00:43
  • Yes I used FirstOrDefault it throwed me this error. Cannot implicitly convert type 'StudentNetwork.Models.StudentDetail' to 'System.Data.Entity.DbSet' – michaeld Apr 17 '13 at 00:54
  • I think you want to assign ViewStudentDetail.Student to a record from the database that matches based on username, right? If so see my updated answer. – kingdango Apr 17 '13 at 00:59
  • Hi kingdango, the above answer is just assigning null but the username provided has details in database, I have edited for more clartiy. – michaeld Apr 17 '13 at 01:19
  • FirstOrDefault will return a record or NULL if one is not found. Perhaps you do not have an exact match? Try testing to see if you can ever retrieve anything, ViewStudentDetail.Student = DbAccess.StudentDetails.First(); – kingdango Apr 17 '13 at 01:23