views:

107

answers:

1

In creating a new ASP.NET MVC application, I have an issue with the approach I'm using to store user-created data for temporary users who have yet to create an account which I then try to convert to a real user. That probably doesn't make much sense, so let me explain:

  • A visitor to the site can enter profile settings before being made to register with a username, password, etc.
  • I'm creating database entries via LINQ to SQL for a new user in this case, using the Request.AnonymousID value as a temporary username.
  • When the user chooses to register, I need to 'switch over' the relevant database records to use the newly entered username instead of the temporary one.

The problem is that when I try to update the record I can't because the username is the primary key, so I'm forced to delete the record and add a new one...

I can probably persevere with this, but I think I might just be going about this in completely the wrong way and wondered if anyone could suggest a better way to allow visitors to store information before they've registered and have that carry over when they do.

I know about profiles but want the profile information to be available to other visitors. I also know that I can create an anonymous profile but it seems like I should be able to keep the data model out of the web.config file.

+1  A: 

I would suggest having an independent primary key for the table with your custom user data. And then have fields like RefAnonymousId and RefUserId to relate that user data to the anonymous user and the registered user, respectively.

For example:

TABLE UserData
(
    UserDataID  int  IDENTITY(1,1)  PRIMARY KEY,
    RefAnonymousId  uniqueidentifier,
    RefUserId  uniqueidentifier,
    ... (data fields),
    (maybe also unique keys on RefUserId and RefAnonymousId)
)

That way you will also be able to identify the user when the user is logged out and maybe automatically log the user in...

Ole Lynge
Oh, I like that. I'll try it and get back to you.
curlyfries
Worked like a charm - thanks!
curlyfries