Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/opencv/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc 如何使用单个post请求更新具有外键关系的两个表-asp.net mvc web api_Asp.net Mvc_Entity Framework_Asp.net Web Api - Fatal编程技术网

Asp.net mvc 如何使用单个post请求更新具有外键关系的两个表-asp.net mvc web api

Asp.net mvc 如何使用单个post请求更新具有外键关系的两个表-asp.net mvc web api,asp.net-mvc,entity-framework,asp.net-web-api,Asp.net Mvc,Entity Framework,Asp.net Web Api,我使用实体框架模型创建了一个asp.net mvc web api,它可以在单个请求中更新单个表。我想要的是在单个POST请求中更新两个表(具有外键关系) 我怎样才能做到这一点 我所做的: 1.在sql server中创建了两个表,分别将EmployeeId作为主键和外键。 2.员工表模型:- public partial class tblEmployeeRecord { public int EmployeeId { get; set; } publi

我使用实体框架模型创建了一个asp.net mvc web api,它可以在单个请求中更新单个表。我想要的是在单个POST请求中更新两个表(具有外键关系)

我怎样才能做到这一点

我所做的:

1.在sql server中创建了两个表,分别将EmployeeId作为主键和外键。 2.员工表模型:-

public partial class tblEmployeeRecord
    {
        public int EmployeeId { get; set; }
        public string Name { get; set; }
        public string userName { get; set; }
        public string userRole { get; set; }
    }
public partial class tblCountryName
    {
        public int CountryId { get; set; }
        public int EmployeeId { get; set; }
        public string Country_Name { get; set; }
       }
3.国家名称表型号:-

public partial class tblEmployeeRecord
    {
        public int EmployeeId { get; set; }
        public string Name { get; set; }
        public string userName { get; set; }
        public string userRole { get; set; }
    }
public partial class tblCountryName
    {
        public int CountryId { get; set; }
        public int EmployeeId { get; set; }
        public string Country_Name { get; set; }
       }
4.创建包装器模型如下:-

 public class UserAndCountry
    {
        public tblEmployeeRecord UserRecord { get; set; }
        public tblCountryName CountryRecord { get; set; }
    }
5.控制器中处理post请求的ActionResult方法:-

[ResponseType(typeof(UserAndCountry))]
        public IHttpActionResult PosttblEmployeeRecord(UserAndCountry tblEmployeeRecord)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }
            db.tblCountryNames.Add(tblEmployeeRecord.CountryRecord);
            db.tblEmployeeRecords.Add(tblEmployeeRecord.UserRecord);
            db.SaveChanges();

            return CreatedAtRoute("DefaultApi", new { id = tblEmployeeRecord.UserRecord.EmployeeId }, tblEmployeeRecord);
        }
6.注意:-发生的情况:-
当我在SQL Server中没有定义这两个表之间的外键关系时,我可以同时更新这两个表,但当我定义外键关系时,我无法更新这些表
在这种情况下,我的请求对象会是什么,我哪里出错?
7.我当前的请求对象:-

{
    "CountryRecord":
{
        "Country_Name": "AP"
},
"UserRecord":
{

        "Name": "Test User",
        "userName": "Test.User@mail.com",
        "userRole": "Hr"
  }
}

实体框架的威力在于关系的导航属性。假设你的模型是:

public partial class Employee
{
    public int EmployeeId { get; set; }
    public string Name { get; set; }
    public string userName { get; set; }
    public string userRole { get; set; }

    // Foreign Key
    public int CountryId { get; set; }
    // Navigation Property
    public virtual Country EmployeeCountry
}

public partial class Country
{
    public int CountryId { get; set; }
    public string Country_Name { get; set; }
}
现在,您的更新变成:

var employeeRecord = tblEmployeeRecord.UserRecord;
// add the country. EF will make the relationship automagically
employeeRecord.EmployeeCountry = tblEmployeeRecord.CountryRecord;
db.Employees.Add(employeeRecord);
db.SaveChanges();

嗯,在一个post请求中可以更新10个表。请更加具体和明确,以便我们能够帮助您。至少举一个例子,说明你正在努力实现的目标!是什么阻止您更新更多表?你能发布一些示例代码吗?@Zhavat&Makotosan更新了我的问题,但我必须更新两个表。这会更新两个表。最重要的是,它还更新了外键。