1
Reply

Adding data to custom table in asp.net identity

Learn AspNet

Learn AspNet

Mar 27 2016 3:13 PM
657

I have added a custom table using asp identity and trying to join that with aspnetusers

public class ApplicationUser : IdentityUser


{

public virtual ICollection<tblAdmin> tblSchoolAdmin { get; set; }

public ClaimsIdentity GenerateUserIdentity(ApplicationUserManager manager)

{

// Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType

var userIdentity = manager.CreateIdentity(this, DefaultAuthenticationTypes.ApplicationCookie);

// Add custom user claims here

return userIdentity;

}

public class tblAdmin

{

[Key]

public Guid AdminID { get; set; }

public string Name { get; set; }

public DateTime Founded { get; set; }

public string UserName { get; set; }

[ForeignKey("UserName")]

public virtual ApplicationUser User { get; set; }

}

public System.Data.Entity.DbSet<tblAdmin> tblAdmindb { get; set; }

Now after I register user using asp net identity, My custom form show up which will put data in my custom data tblAdmin that I created.

My Stored Procedure:-

CREATE PROCEDURE [dbo].[Procedure]
    @AdminID    Varchar(50),
    @SchoolName Varchar (50),
    @Founded Date,
    @UserName nvarchar(128)
 
AS
BEGIN
    INSERT INTO tblAdmins(AdminID,Name,Founded,UserName) values (@AdminID,@SchoolName,@Founded,@UserName)
END
 

My Custom page code to call Stored Procedure

protected void Button1_Click(object sender, EventArgs e)
        {
 
            var userName = HttpContext.Current.User.Identity.Name;
            //var UserId = HttpContext.Current.User.Identity.GetUserId();
            SqlConnection mySqlConnection = new SqlConnection("Data Source=.;Initial Catalog=AspNetIdentityFinal;Integrated Security=True");
 
 
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = mySqlConnection;
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "Procedure";
 
            //adding paramerters to  SqlCommand below 
            Guid g;
 
            g = Guid.NewGuid();
            cmd.Parameters.AddWithValue("@AdminID", g);
            cmd.Parameters.AddWithValue("@SchoolName", TextBox1.Text.ToString());        //first Name
            cmd.Parameters.AddWithValue("@Founded ", TextBox2.Text);     //middle Name
            cmd.Parameters.AddWithValue("@UserName ", userName);
 
            try
            {
                mySqlConnection.Open();
                cmd.ExecuteNonQuery();
                Label1.Text = "Row inserted sucessfully";
            }
            catch (Exception ex)
            {
                Label1.Text = ex.Message;
            }
            finally
            {
                mySqlConnection.Close();
                mySqlConnection.Dispose();
            } 
 
        }
 
 

I am trying to use Stored procedure but it keeps giving me FOREIGN Key issue because of Column User ID in table AspNetUsers. What should I do to put entry in User_ID in tblAdmins which shows as Foreign key but cannot be used in Procedure?


Answers (1)