Resolved ASP.NET Code First Paradox

etkmlm

Member
Joined
Sep 14, 2020
Messages
8
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:

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
3,217
Location
Sydney, Australia
Programming Experience
10+
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.
 

etkmlm

Member
Joined
Sep 14, 2020
Messages
8
Programming Experience
3-5
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.
 

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
3,217
Location
Sydney, Australia
Programming Experience
10+
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?
 

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
1,959
Location
Chesapeake, VA
Programming Experience
10+
@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.
 

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
1,959
Location
Chesapeake, VA
Programming Experience
10+
Moved into Entity Framework since this is only an issue for Entity Framework. It's not a generic .NET Framework issue.
 

etkmlm

Member
Joined
Sep 14, 2020
Messages
8
Programming Experience
3-5
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.
 

Sheepings

Senior Programmer
Staff member
Joined
Sep 5, 2018
Messages
1,679
Location
UK
Programming Experience
10+
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
 

etkmlm

Member
Joined
Sep 14, 2020
Messages
8
Programming Experience
3-5
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.
 

Sheepings

Senior Programmer
Staff member
Joined
Sep 5, 2018
Messages
1,679
Location
UK
Programming Experience
10+
No problem. That's quite Ok, language transliterations are not always perfect. I will also consider that as a possibility regarding future replies. (y)
 
Top Bottom