Pablo Fernandez
Pablo Fernandez

Reputation: 287400

How to handle relationships to users from other tables in ASP.Net MVC?

At the table level I'm setting aspnet_User.UserID as foreign key of UserID in the other tables (like Posts.UserID, so each post has an owner). Is that the way to go?

When I create the LINQ to SQL model, should I include the aspnet_User table?

When I create a post (record in the Posts table, a Post object), how do I set the relationship to the logged in user (User object in the controller)?

Upvotes: 1

Views: 468

Answers (2)

Pure.Krome
Pure.Krome

Reputation: 86957

In your database schema, you should definately have a the UserID in the Post table be a foreign key to the aspnet_user table, on the UserID field. This way, you are making sure your data is clean. I would even add a cascade delete & update on that relationship.

Then, refer to my instructions in a previous question you asked, about how to get the user data.

(Thomas Stock summed it up briefly, above, though :) )

Upvotes: 0

Thomas Stock
Thomas Stock

Reputation: 11256

I don't include the aspnet_User table to my linq-to-sql as I don't really need it. I use the built in way of accessing membership data.

But for your table Posts, it might be easier for you to include it so that you can easily display the User's Name by doing myPost.User.Name

edit:

    MembershipUser user = Membership.GetUser();
    Guid userGuid = (Guid)user.ProviderUserKey;

    Post post = new Post
            {
                 UserId =userGuid,
                 Message = message                                      
            };

Upvotes: 1

Related Questions