OnModelCreating vs DataAnnotations for database setup

I have an ntier solution that has a Domain project for my POCO classes. I want to create a database with some table fields configured for a specific length. Now I see two ways to do this: either data annotations in the POCO classes themselves, or using the OnModelCreating method in my DbContext, but the best way?

My problems with the OnModelCreating approach are that this is specific to the Entity Framework. If I switch to another ORM, the database congruence will be lost if I do not repeat its implementation.

With the Annotations approach, I eventually clutter up my models and worry that they lose the POCO tag. I'm also not sure that other ORMs will respect Annotations. On the other hand, all the information about how the database should look like is tied to a model that is easy to maintain.

Any thoughts pointing me in the right direction?

+4
source share
1 answer

OnModelCreating better. Some display features are not available through data annotations. Moreover, as you already mentioned, in some cases, data annotations are contrary to the POCO principle, because your classes may contain persistence information, and even some data annotations currently need the EntityFramework.dll referenced in your domain project.

Also keep in mind that data annotations are a feature specific to MS, used mainly by MS tools. If you want to use them with other ORMs, you will have to implement the conversion from data annotations to the description of the ORM mapping (if it does not already exist).

+3
source

Source: https://habr.com/ru/post/1402874/


All Articles