Custom Membership in ASP.NET MVC 2

Posted by Interfector on Stack Overflow See other posts from Stack Overflow or by Interfector
Published on 2010-06-12T14:56:05Z Indexed on 2010/06/12 15:03 UTC
Read the original article Hit count: 442

Hello,

I'm trying to make my Custom Membership starting from ASP.NET's. I have a users table created by me which has as Primary Key a Guid UserId, exactly the sames as ASP.NET's default user and membership table. I have all the Foreign Key relationships built. However, I cannot insert the user into my custom users table. The data gets inserted correctly into the default ASP.NET's tables. I have tried the following scenarios:

  1. First version

    • Receive the user model from POST
    • call the CreateUser method of the Membership.class (stuff gets inserted)
    • without modifying the user object, I try to insert it using EF's AddObject
    • I receive the following error:

      "Cannot insert the value NULL into column 'LoweredUserName', table 'asp.dbo.aspnet_Users'; column does not allow nulls. INSERT fails.\r\nThe statement has been terminated."} System.Exception {System.Data.SqlClient.SqlException

    The error actually makes sense as EF's AddObject doesn't know how to create the LoweredUserName value, and I don't want to do it, this is ASP.NET's job.

  2. Second version

    • Receive the user model from POST
    • call the CreateUser method of the Membership.class (stuff gets inserted)
    • Get the Guid of the freshly inserted system user
    • get the ASP.NET's User object and overwrite that of user.User
    • get the ASP.NET's Membership object and overwrite that of user.Membership (remember the FKs)
    • try again EF's AddObject
    • new error

    {"The relationship between the two objects cannot be defined because they are attached to different ObjectContext objects."} System.Exception {System.InvalidOperationException}

    Don't know what this is, but it certenly doesn't make any sense for me.

After some googleing I think that it has something to do with the context, but as I'm a beginner I don't know how to fix it.

Any ideeas on how to accomplish this task are more than welcomed, especially if they follow some good practices.

Thx

© Stack Overflow or respective owner

Related posts about asp.net-mvc-2

Related posts about asp.net-membership