Entity Framework virtual property conflict

I do not understand why this works everywhere, but in this place.

private usersliked createUserLiked(post post)
    {
        usersliked newUsersLiked = db.usersliked.Create();
        newUsersLiked.postId = post.idpost;
        int length = db.user.Count();
        user user = db.user.OrderBy(c => c.iduser).Skip(randomizer.Next(1, length)).FirstOrDefault();
        newUsersLiked.userId = user.iduser;
        db.usersliked.Add(newUsersLiked);
        db.SaveChanges();
        return newUsersLiked; 
    }

I want to add a new entry userLikedthat has some foreign keys for postand user. A virtual foreign key class for useris created EFafter adding idUser. But using the same method for a link postdoes not work! And, since the "post" graph in the table is not NULL, this gives me an error. As you can see in the screenshot, the virtual class is null. Why?

enter image description here

UPDATE: If I manually set the message, then this will ruin the link user. Somehow it copies postId to userId, and everything slows down.

enter image description here

UPDATE: The problem is resolved. It was a mistake in foreign keys; they all referred to one common subject.

+4

All Articles