I want to insert data into multiple related tables, I searched, but everything I've found is insert data into multiple tables that aren't related. I have two tables User and OrchardTable, both are related by the user Id, so the primary key of the User table is the Id, which is generated as an identity on the database, and the foreign key of the table OrchardTable is the User Id.

This is from my controller

```
[HttpPost]
public ActionResult post([FromBody] Models.Users u)
{
    using (Models.OrchardAdminContext db = new Models.OrchardAdminContext())
    {
        Models.User oUser = new Models.User();

        Models.OrchardTable userOrchar = new Models.OrchardTable();

        oUser.Name = u.Name;
        oUser.Email = u.Email;
        db.User.Add(oUser);
        db.SaveChanges();

        var idUser = db.OrchardTable.Where(x => x.email = oUser.Email).FirstOrDefault();
        userOrchar.IdUser = idUser.Id;
        userOrchar.orchardUbication = u.ubication;
        db.OrchardTable.Add(userOrchard);
        db.SaveChanges();
    }
    return Ok();
}

```

So, the code above do: first adds an user to the User table, on the SQL Server Management Studio the Id of each user is an identity therefore when the data is saved the Id is generated and through the mail the id of the user is recovered to make the relation between the tables User and OrchardTable by the Id.

I think this is a way of do it, but not the best. Besides the main question, I want to know if there are a better and correct way to do the insert.

Any help is appreciated.

UPDATE
Stored procedure to insert into the table User and return the Id from the same instert:

```
GO
SET QUOTED_IDENTIFIER ON
GO

CREATE PROCEDURE spUserInsert 
   @name varchar(20),
   @email varchar(100)
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;

-- Insert statements for procedure here
   insert into user(name, email) values (@name, @email);
   DECLARE @Id INT  
   SET @Id=@@IDENTITY  
   SELECT @Id AS id
END
GO

```

So if I'm not mistaken, the idea is to create an SP to insert and return the id of that insert, then with the id send it to another SP where it is inserted into OrchardTable. But now when I call it from the controller it doesn't seem to work because no saved record shows up in the database.

The controller:

```
    [HttpPost]
    public ActionResult post([FromBody] Models.Users u)
    {
        using (Models.OrchardAdminContext db = new Models.OrchardAdminContext())
        {
            Models.User oUser = new Models.User();

            Models.OrchardTable userOrchar = new Models.OrchardTable();

            oUsuar.Name = u.Name;
            oUsuar.Email = u.Email;

            var idUserReg = db.User.FromSqlRaw("Execute spUserInsert @name, @email", oUsuar.Name, oUsuar.Email);

            db.SaveChanges();

        }
        return Ok();
    }

```

I tested the stored procedure on Sql Server and works, but on the controller not, and I don't know why on the controller din't work.
Again, any help is appreciated.