简体   繁体   中英

ASP.NET Core API model concept

I'm working on a sample app that exposes a restful API. I'm also using EF core to map entities mkd to a SQLite db.

Each entity has an ID property that is auto-generated and is unique.

The api expose a method for the creation of an entity by providing a model object. At the moment I'm using the same model that is mapped to EF Core (with the ID field included).

I honestly don't think that exposing the ID field during the creation of an entity is a good practice, so I thought to create another model without the ID field that will be passed to the create method and then map it the model that EF is using.

Is it a good practice or is it too overkill?

Am I missing something?

What you are essentially doing is the use of a DTO/ViewModel instead of the model object directly, and that is considered a good practice. It saves you from the problem of over-posting as well. Alternatively, you can choose to Bind specific properties as well - refer to https://www.hanselman.com/blog/ASPNETOverpostingMassAssignmentModelBindingSecurity.aspx

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM