Inserting into multiple Table using Entity Framework in ASP.Net

Last Reply one year ago By Mahesh1986

Posted one year ago

Hi ,

 I have a registration page fields are : username , Address , phoneno.

i have 3 tables in data base :

User : user id primary key , username

Address : user id primary key , address 

Contact : user id , phoneno

i am using entity frame work so i got 3class defaultly.

now requirement is when i click on register button those fields i have to insert into those  3 tables in a proper way

Please help regarding this 

Posted one year ago Modified on one year ago

Hi,

Please refer below code.

HTML

<div>
    <table border="0" cellpadding="0" cellspacing="0">
        <tr>
            <td>
                Name
            </td>
            <td>
                <asp:TextBox ID="txtName" runat="server" />
            </td>
        </tr>
        <tr>
            <td>
                Address
            </td>
            <td>
                <asp:TextBox ID="txtAddress" runat="server" />
            </td>
        </tr>
        <tr>
            <td>
                Phone Number
            </td>
            <td>
                <asp:TextBox ID="txtPhoneNumber" runat="server" />
            </td>
        </tr>
    </table>
    <br />
    <asp:Button Text="Save" runat="server" OnClick="Save" />
</div>

C#

protected void Save(object sender, EventArgs e)
{
    using (UserModel.UserContext context = new UserModel.UserContext())
    {
        var user = context.InsertUser(null, txtName.Text);
        int? userId = user.FirstOrDefault();
        if (userId != null && userId > 0)
        {
            context.AddToAddressEntities(new UserModel.AddressEntity
            {
                UserId = (int)userId,
                Address = txtAddress.Text.Trim()
            });
            context.AddToContactEntities(new UserModel.ContactEntity
            {
                UserId = (int)userId,
                PhoneNo = Convert.ToInt32(txtPhoneNumber.Text)
            });
        }
        context.SaveChanges();
    }
}

SQL

CREATE PROC [dbo].[InsertUser]
	@UserId INT= NULL,
	@UserName VARCHAR(50) 
AS
BEGIN
	IF NOT EXISTS(SELECT UserId FROM [UserEntity] WHERE UserId = @UserId)
	BEGIN
		INSERT INTO [UserEntity]([UserName])
		VALUES(@UserName)

		SELECT UserId FROM [UserEntity] WHERE UserId = SCOPE_IDENTITY();
	END
	ELSE
	BEGIN
		UPDATE [UserEntity]
		SET UserName = @UserName
		WHERE UserId = @UserId
	END
END

Screenshot

 


Posted one year ago

i Used the Following Code and Achieved my functionality

 

  private bool addUser(UserObject user)
        {
            UserDetail ud = new UserDetail();
            ud.EmpName = user.Name;
            ud.Gender = "Male";
            UserContact uc = new UserContact();
            uc.Phone = user.Phone;
            
            UserAddress ua = new UserAddress();
            ua.Address1 = user.Address1;
            ua.Address2 = "Guntur";
            db.UserDetails.Add(ud);

            db.UserAddresses.Add(ua);
            db.UserContacts.Add(uc);
            int i = db.SaveChanges();
            if (i > 1)
            {
                return true;
            }
            return false;

Thank You!!!!

I agree, here is the link: https://www.e-iceblue.com/Introduce/spire-office-for-net-free.html