Efcore: Explanation of HasAnnotation in fluent configuration

Created on 16 Aug 2018  路  2Comments  路  Source: dotnet/efcore

https://docs.microsoft.com/en-us/dotnet/api/microsoft.entityframeworkcore.modelbuilder.hasannotation?view=efcore-2.1

How exactly does it work? What is exactly annotation in this method's context? Is this database related or application related?

Does it mean I can add attributes like [MyAttribute] using fluent API?

closed-question customer-reported

Most helpful comment

@Thorgard It's an arbitrary object attached to the model and referenced by a name. It's not a "data annotation" or a .NET attribute. It's just a way of attaching information to the model that can later be read from the model. Internally, lots of things configured on the model are stored as annotations--for example, the constraint name for an FK.

You are free to add your own annotations, but they have little value unless you implement something that also uses them, and that's not common. One place they can be useful is to flow information from model building through to Migrations--issue #10258 is about making that easier to use.

All 2 comments

@Thorgard It's an arbitrary object attached to the model and referenced by a name. It's not a "data annotation" or a .NET attribute. It's just a way of attaching information to the model that can later be read from the model. Internally, lots of things configured on the model are stored as annotations--for example, the constraint name for an FK.

You are free to add your own annotations, but they have little value unless you implement something that also uses them, and that's not common. One place they can be useful is to flow information from model building through to Migrations--issue #10258 is about making that easier to use.

Was this page helpful?
0 / 5 - 0 ratings