Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.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
C# 在WebAPI2中定义复合主键的路由_C#_Entity Framework_Asp.net Web Api_Composite Primary Key_Asp.net Web Api Routing - Fatal编程技术网

C# 在WebAPI2中定义复合主键的路由

C# 在WebAPI2中定义复合主键的路由,c#,entity-framework,asp.net-web-api,composite-primary-key,asp.net-web-api-routing,C#,Entity Framework,Asp.net Web Api,Composite Primary Key,Asp.net Web Api Routing,如何在WebAPI控制器中定义复合PK?我有两个实体。其中一个我有两个主键 实体“Person”具有以下属性:Id(PK)、Lname(PK)、Fname、Age、GroupId(FK) 这是我的控制器代码: // GET: api/Person/5 [HttpGet] [Route("Id")] [ResponseType(typeof(Person))] public async Task<IHttpActionResult> GetPersonById(int id, stri

如何在WebAPI控制器中定义复合PK?我有两个实体。其中一个我有两个主键

实体“Person”具有以下属性:Id(PK)、Lname(PK)、Fname、Age、GroupId(FK)

这是我的控制器代码:

// GET: api/Person/5
[HttpGet]
[Route("Id")]
[ResponseType(typeof(Person))]
public async Task<IHttpActionResult> GetPersonById(int id, string lname)
{
    Person person = await db.Persons.FindAsync(new object[] {id, lname});
    if (person == null)
    {
       return NotFound();
    }

    return Ok(person);
}

我是否也必须在Route DataAnnotation中定义lname?

您是说实体框架吗?如何在其中设置复合主键?顺便说一句,你们班的人很奇怪。。。组的外键字段是Id而不是GroupId?@bubi是的,我用实体框架创建了模型。但是我单独定义的数据注释。是的,我正在使用对象组中的外键,该对象组包含组实体的属性。
public partial class Person
{
  public int Id { get; set; }
  public string lname { get; set; }
  public string fname { get; set; }
  public int Age { get; set; }

  public int GroupId { get; set; }

  [ForeignKey("Id")]
  public virtual Group Group { get; set; }
}