Data annotations entity framework9/17/2023 Learning the Fluent API is almost a Must imho, the DataAnnotations are a nice-to-have for simple applications. But if you don't want to be forced by the mapping conventions when you define your object model, your only option then is the Fluent API. Currently you cannot overwrite the conventions (only disable them MS announced to give configuration options for the conventions in future EF releases). If you cannot fine tune your model with DataAnnotations anymore your last resort is to follow the default mapping conventions (by naming your properties according to those rules). Specification of inheritance mapping between object model and database tables (Table-Per-Hierarchy, Table-Per-Type, Table-Per-Concrete-Class):Įdit: Microsoft considers the Fluent API as an "advanced feature" (Quote from here):īut in my opinion you reach the limitations of DataAnnotations very quickly (except perhaps for extremely simple object models). WithMany(.), WithOptional(.), WithRequiredDependent(.), WithRequiredPrincipal(.) Map(conf => conf.MapKey("MyForeignKeyID"))įine granular tuning of relationships, especially in all cases where only one side of an association is exposed in the object model: Specify foreign key column name in the database when the key isn't exposed in your object model: Attributes are a kind of tag that you can place on a class or property to specify metadata about that class or property. Configuration can be applied in two ways, using the Fluent API, and through DataAnnotation attributes. So, from the viewpoint of configuration options and flexibility the Fluent API is "better".Ĭonfiguration examples (for sure not a full list) which are possible in the Fluent API but not with DataAnnotations (as far as I can see): EF Core Data Annotations Configuration enables you to override EF Core's default behaviour. Everything what you can configure with DataAnnotations is also possible with the Fluent API.
0 Comments
Leave a Reply.AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |