ASPForums.Net RSS Feedhttp://www.aspforums.net/Handlers/RSS.ashxLatest additions to the content that appears on ASPForums.Net(c) 2017 www.aspforums.com. All rights reserved.Inserting into multiple Table using Entity Framework in ASP.Net<p>Hi,</p> <p>Please refer below code.</p> <p><span style="text-decoration: underline;">HTML</span></p> <pre class="brush: csharp">&lt;div&gt; &lt;table border=&#34;0&#34; cellpadding=&#34;0&#34; cellspacing=&#34;0&#34;&gt; &lt;tr&gt; &lt;td&gt; Name &lt;/td&gt; &lt;td&gt; &lt;asp:TextBox ID=&#34;txtName&#34; runat=&#34;server&#34; /&gt; &lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt; Address &lt;/td&gt; &lt;td&gt; &lt;asp:TextBox ID=&#34;txtAddress&#34; runat=&#34;server&#34; /&gt; &lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt; Phone Number &lt;/td&gt; &lt;td&gt; &lt;asp:TextBox ID=&#34;txtPhoneNumber&#34; runat=&#34;server&#34; /&gt; &lt;/td&gt; &lt;/tr&gt; &lt;/table&gt; &lt;br /&gt; &lt;asp:Button Text=&#34;Save&#34; runat=&#34;server&#34; OnClick=&#34;Save&#34; /&gt; &lt;/div&gt;</pre> <p><span style="text-decoration: underline;">C#</span></p> <pre class="brush: csharp">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 &amp;&amp; userId &gt; 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(); } }</pre> <p><span style="text-decoration: underline;">SQL</span></p> <pre class="brush: 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</pre> <p>Screenshot</p> <p><img src="http://i988.photobucket.com/albums/af3/mudassarkhan/InsertIntoMultipleEntity.gif" alt="" width="348" height="339" /></p> <p>&nbsp;</p>http://www.aspforums.net/Threads/134214/Inserting-into-multiple-Table-using-Entity-Framework-in-ASPNet/http://www.aspforums.net/Threads/134214/Inserting-into-multiple-Table-using-Entity-Framework-in-ASPNet/Mon, 29 Feb 2016 09:59:07 GMTInserting into multiple Table using Entity Framework in ASP.Net<p>i Used the Following Code and Achieved my functionality</p> <p>&nbsp;</p> <pre class="brush: csharp"> private bool addUser(UserObject user) { UserDetail ud = new UserDetail(); ud.EmpName = user.Name; ud.Gender = &#34;Male&#34;; UserContact uc = new UserContact(); uc.Phone = user.Phone; UserAddress ua = new UserAddress(); ua.Address1 = user.Address1; ua.Address2 = &#34;Guntur&#34;; db.UserDetails.Add(ud); db.UserAddresses.Add(ua); db.UserContacts.Add(uc); int i = db.SaveChanges(); if (i &gt; 1) { return true; } return false;</pre> <p>Thank You!!!!</p>http://www.aspforums.net/Threads/134214/Inserting-into-multiple-Table-using-Entity-Framework-in-ASPNet/http://www.aspforums.net/Threads/134214/Inserting-into-multiple-Table-using-Entity-Framework-in-ASPNet/Mon, 29 Feb 2016 22:42:30 GMT