These tools provide a graphical tool which builds on top of the EF Core command line tools and offers additional workflow and customization options. The long view can be accessed from code as well: Console.WriteLine((MetadataDebugStringOptions.If you use Visual Studio, try out the EF Core Power Tools community extension. The long form also includes all the annotations, which could be useful if you need to view relational or provider-specific metadata. The debug view has a short form and a long form. It can also be accessed directly from code, for example to send the debug view to the console: Console.WriteLine(()) The model builder debug view can be accessed in the debugger of your IDE. When desired, indexes can still be explicitly created for foreign key columns, either using the IndexAttribute or with configuration in OnModelCreating. Post ToDependent: Posts ToPrincipal: Blog Cascade Id (int) Required PK AfterSave:Throw ValueGenerated.OnAddĪuthorId (no field, int?) Shadow FK IndexīlogId (no field, int) Shadow Required FK IndexĪuthor (Author) ToPrincipal Author Inverse: Postsīlog (Blog) ToPrincipal Blog Inverse: Posts Looking at the model debug view for a Post entity type with relationships to Blog and Author, we can see two indexes are created-one for the BlogId FK, and the other for the AuthorId FK. It usually makes sense to create indexes for foreign key (FK) columns, and hence there is a built-in convention for this: ForeignKeyIndexConvention. Example: Don't create indexes for foreign key columns If your model doesn't use mapping attributes (aka data annotations) for configuration, then all conventions with the name ending in AttributeConvention can be safely removed to speed up model building. However, that list doesn't include conventions introduced by third-party database providers and plugins.Īpplications can remove or replace any of these conventions, as well as add new custom conventions that apply configuration for patterns that are not recognized by EF out of the box. You can find all of them in the list of classes that implement the IConvention interface. Namespace ĮF Core includes many model building conventions that are enabled by default. Data annotations will override conventions, but will be overridden by Fluent API configuration. You can also apply certain attributes (known as Data Annotations) to your classes and properties. Use data annotations to configure a model Entity types must be added to the model before the attribute will be discovered on that entity type. Or by registering it in OnModelCreating: protected override void OnModelCreating(ModelBuilder modelBuilder)ĮntityTypeConfigurationAttribute types will not be automatically discovered in an assembly. For example, by creating a DbSet property for the entity type: public class BooksContext : DbContext The entity type is included in a model using one of the normal mechanisms. This attribute means that EF Core will use the specified IEntityTypeConfiguration implementation whenever the Book entity type is included in a model. Rather than explicitly calling Configure, an EntityTypeConfigurationAttribute can instead be placed on the entity type such that EF Core can find and use appropriate configuration. Using EntityTypeConfigurationAttribute on entity types The order in which the configurations will be applied is undefined, therefore this method should only be used when the order doesn't matter. Protected override void OnModelCreating(ModelBuilder modelBuilder) The configuration is applied in the order the methods are called and if there are any conflicts the latest call will override previously specified configuration. Fluent API configuration has the highest precedence and will override conventions and data annotations. This is the most powerful method of configuration and allows configuration to be specified without modifying your entity classes. You can override the OnModelCreating method in your derived context and use the fluent API to configure your model. You can view this article's samples on GitHub.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |