0

I have a table Users that has IDCompany and IDUserCode as its primary key.

But when I scaffold for New->Controller, it always generates only one field (id) as key:

public class UsersController : ApiController
{
      private CaiqueEntities db = new CaiqueEntities();

      // GET: api/Users
      public IQueryable<Users> GetUsers()
      {
         return db.Users;
      }

      // GET: api/Users/5
      [ResponseType(typeof(Users))]
      public IHttpActionResult GetUsers(int id)
      {
         Users users = db.Users.Find(id);

         if (users == null)
         {
            return NotFound();
         }

         return Ok(users);
      }
   }

I need, the first GET to receive IDCompany, and the second IDCompany AND IDUser.

Is there any way to configure VS to generate this way? Or I need to do it manually?

Already tested:

  • [Key] attributes
  • New route
  • Override OnModelCreating

Second day testing EF tutorials.

Thanks in advance.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Nane
  • 318
  • 1
  • 8
  • 18
  • Does this answer your question? [composite key resource REST service](https://stackoverflow.com/questions/16180147/composite-key-resource-rest-service) – Bryan Dellinger Jan 21 '20 at 01:25
  • @BryanDellinger. I need to read it a little more to see if it fits my needs. Seems like an awesome tip. Thanks for your fast response. Is not automatic scaffolding, but can work. Anyway, I'll let you know if is that to put it as answer. – Nane Jan 21 '20 at 01:40
  • @BryanDellinger. For one part, the answer is NO. There is no way that scaffolding takes both fields of the PK. For doing that, I need to change the template (https://www.hanselman.com/blog/ModifyingTheDefaultCodeGenerationscaffoldingTemplatesInASPNETMVC.aspx). I'm not prepared to do that yet. So I will explore your option. I'll let you know – Nane Jan 21 '20 at 02:15

0 Answers0