ASP.NET Core API model concept - c#

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

Related

How to wire Entity Framework Core to use invariant-enforcing constructor

I have entities on which I don't want to have a default constructor because the constructor has logic and enforces invariants.
I would like to be able to configure Entity Framework Core to use a specific constructor in a certain way.
The workaround I use for now is have purely data-oriented classes (sort of "Database DTOs") for mapping entities to database, but this is not very satisfying I would like to use my domain entity classes with EF Core.
I know that EF Core can use a constructor having parameters matching the properties. But it is too limited for some use cases. For instance, I have an entity which implements the State design pattern, meaning when retrieved from the database, a state class must be recreated from a string using a factory. EF Core can't figure that on its own and I've not yet a way to configure how entities are constructed when reconstituted from the database.
Do you know if it is possible in EF Core ?
Thanks you in advance,

Entity Framework 6 - Database First - String Length Attribute missing

I am using EF6 database first to create my EF 6 DB Context. The POCOs that are created are missing the property attributes such as "StringLength" or "Required". I have been on projects before where these were created for me and thought it was base functionality. Where can I find documentation on how to configure this behavior?
From a new Winform application I added a new ADO.Net Entity Data Model to my project and followed the wizard for "EF Design from database".
Environment
New .Net Framework 4.72 Winform app
EF 6
C#
I found out what happened. By default Scaffold-DBContext's parameter DataAnnotations defaults to use Fluent API. Fluent API configures the properties using function chaining in the OnModelCreating method of the context. The DataAnnotations parameter is a switch, so just specifying the parameter will change the engine to use Data Annotations.
This is important to be because I use the EF Models outside of EF as well. There is a big benefit using them with ASP.net because it will use the annotations during request to validate the models.

Are DTO Classes inherently included when using the .Net Core Entity Framework Code First approach?

Using the Database-First EF model my DTO classes had a clear separation from the actual Database models created by EF. This provisioned a flexible architecture as the DTO and model weren't tightly coupled.
Using Code-First (which I've typically not used as often); I have come across a scenario where when I am adding a migration I get an error:
The Entity Type '[EntityTypeName]' requires a primary key to be defined.
The EntityType in this case is actually defined within one of my DTO classes and isn't intended to be the actual replica of any field in the corresponding Database model. It is a class created to facilitate Serialization data transfer of a JSON object from my API to the database.
But the real question is what concern or relevance is that of EF as it does not specifically relate to the underlying Database Model?
Is there something or someway I can alleviate the error to add my migration? Or is this a default behavior when using the .NET Core Code-First approach that can possibly be disabled? The build of the Solution is fine, it's just EF complaining.

Database First Entity Framework

I am developing a web-based system using .NET MVC and Entity Framework, from a legacy system. I will keep the legacy database and only develop the application, so I will create entity classes using database first approach.
My concern is, in the entity classes, I will be adding attributes such as filter attributes. And I may add some extra fields there as well. Will the database be updated automatically? I actually don't want to update the database at all. I only want extra stuff stay with the entity class only.
You can easily create your database first and then start working on the backend side of your application as you would do it in an "Code first" approach.
I'd recommend reading this article, as you might get all your answers cleared with some examples: Entity framework
First, when adding additional properties in your entity classes you will want to mark them for EF to ignore using either the [NotMapped] attribute or in your DbContext class calling .Ignore on the property. Not Mapped Attribute
Second, in terms of EF changing your database you can do a couple of things.
1. Ensure the account that is updating data does not have Create,Alter,etc permissions in the environment
2. Use a null database initializer at the beginning of your application. Database.SetInitializer(null); Setting the initializer to null tells EF not to make any sort of changes to the database. Turn off DB Initializer
Maybe you should consider using partial classes.
I find editing anything in EF quite hard.
EF Classes from your DB are all :
public partial myClass(){
public string myDBProperty;
}
Which means you are able to create a second class in the same namespace like :
public partial myClass(){
public string myOwnProperty;
}

Using database first entity with code first entity in entity framework

I am working in an asp.net MVC 4 application and I am using Entity framework 6 in my application. I am using both code first approach for new tables/entities as well as database first approach with model designer(edmx). Customers are in codefirst model with separate context where as vehicles are in edmx. both have different context object. I want to use a query like this:
return View(maindb.Reservations.Include("customer").Include("Vehicle"));
but it returns error:
A specified Include path is not valid. The EntityType 'myproject.Data.Reservation' does not declare a navigation property with the name 'Vehicle'.
Please suggest how to fix it so that I can get properties of Vehicle and use them in my view.
I am using both code first approach for new tables/entities as well
as database first approach with model designer(edmx)
Wrong way to go.
Please suggest how to fix it
Put those entities into the same context.
You need to choose which approach is more suitable for you. For example, code first works as well with existing tables. If your tables don't match the naming conventions, you can easily override them with data annotations or fluent API, which is very flexible (see http://msdn.microsoft.com/en-us/data/jj591617 and http://msdn.microsoft.com/en-us/data/jj591620).

Categories