Resolved ASP.NET Code First Paradox

etkmlm

Member
Joined
Sep 14, 2020
Messages
9
Programming Experience
3-5
Hi, I'm using MVC5 with .NET 4.6.1.

I want to users can follow each other but don't know how can I do this.. I tried these things:

C#:
public class User
    {
        [Key]
        public int ID { get; set; }
        public String Avatar { get; set; }
        public String State { get; set; }
        public String Mail { get; set; }
        public String Username { get; set; }
        public String Password { get; set; }
        public ICollection<Post> Posts { get; set; }
        public IEnumerable<User> Follows { get; set; }
        public String RegDate { get; set; }
        public IEnumerable<Post> LikesPost { get; set; }
        public IEnumerable<Post> DislikesPost { get; set; }
        public IEnumerable<Comment> LikesComment { get; set; }
        public IEnumerable<Comment> DislikesComment { get; set; }

        public IEnumerable<Label> FavLabels { get; set; }
        public int Report { get; set; }
    }

but changes are not reflected in the database. Tried ICollection but it creates a ton of relates.
 
Last edited:
Tried ICollection but it creates a ton of relates.
I don't know what that means. Does your Posts property work as desired?

By the way, rather than Follows, LikesPost, DislikesPost and LikesComment, I would suggest that those properties should be named FollowedUsers, LikedPosts, DislikedPosts and LikedComments.
 
I don't know what that means. Does your Posts property work as desired?

By the way, rather than Follows, LikesPost, DislikesPost and LikesComment, I would suggest that those properties should be named FollowedUsers, LikedPosts, DislikedPosts and LikedComments.

Whenever I use ICollection, SQL automatically creates unnecessary related tables. Yeah, it works fine.

Finally, thanks for suggests.
 
Whenever I use ICollection, SQL automatically creates unnecessary related tables.
They're probably not unnecessary though. In the case of that Posts property, presumably the Post class has a UserId property because it's a one-to-many relationship, i.e. each Post has one User that owns it and each User can have many Posts that it owns. The others are many-to-many relationships though, e.g. each User can follow many other Users and also be followed by many other Users. In the database, that requires an extra table because you need one record for each combination of following and followed User. If you had a User following multiple other Users and being followed by multiple other Users, how exactly do you think it could be represented without an extra table?
 
@etkmlm: in the future, please post your code in code tags instead of a screenshot. Screenshots are hard to read on mobile devices and makes it harder for the volunteers to copy and paste your code if they want to help you.
 
Moved into Entity Framework since this is only an issue for Entity Framework. It's not a generic .NET Framework issue.
 
They're probably not unnecessary though. In the case of that Posts property, presumably the Post class has a UserId property because it's a one-to-many relationship, i.e. each Post has one User that owns it and each User can have many Posts that it owns. The others are many-to-many relationships though, e.g. each User can follow many other Users and also be followed by many other Users. In the database, that requires an extra table because you need one record for each combination of following and followed User. If you had a User following multiple other Users and being followed by multiple other Users, how exactly do you think it could be represented without an extra table?

Thanks, I will try.

@etkmlm: in the future, please post your code in code tags instead of a screenshot. Screenshots are hard to read on mobile devices and makes it harder for the volunteers to copy and paste your code if they want to help you.

Ok, I will consider it.
 
Lol you will consider it? :LOL:

There is nothing to consider. If you repeatedly post topics with code and without code tags as demonstrated below, it may result in a moderator closing or removing your topic. And we really don't want to do that on you. :)

It's quite a simply polite request which helps us to help you faster. Adding code tags has a purpose. It makes your code readable, retains the markup formatting, and its one of the very few requirements we do have around here.

CodeTags.gif
 
Ok, I will consider it.
More people may consider reading your questions if you do, and that increases the chance of getting help.
 
Lol you will consider it? :LOL:

There is nothing to consider. If you repeatedly post topics with code and without code tags as demonstrated below, it may result in a moderator closing or removing your topic. And we really don't want to do that on you. :)

It's quite a simply polite request which helps us to help you faster. Adding code tags has a purpose. It makes your code readable, retains the markup formatting, and its one of the very few requirements we do have around here.

View attachment 1100

Sorry,
I checked the meaning from google translate. I didn't mean it in this sense.

After that I will check the translations again and again, thank you. And actually what I meant was I wouldn't do it again.
 
No problem. That's quite Ok, language transliterations are not always perfect. I will also consider that as a possibility regarding future replies. (y)
 
Back
Top Bottom