创建并配置模型Creating and configuring a model
10/13/2020
本文内容
Entity Framework 使用一组约定基于实体类的形状构建模型。Entity Framework uses a set of conventions to build a model based on the shape of your entity classes. 可指定其他配置以补充和/或替代约定的内容。You can specify additional configuration to supplement and/or override what was discovered by convention.
本文介绍可应用于面向任何数据存储的模型的配置,以及面向任意关系数据库时可应用的配置。This article covers configuration that can be applied to a model targeting any data store and that which can be applied when targeting any relational database. 提供程序还可支持特定于具体数据存储的配置。Providers may also enable configuration that is specific to a particular data store. 有关提供程序特定配置的文档,请参阅数据库提供程序部分。For documentation on provider specific configuration see the Database Providers section.
提示
可在 GitHub 上查看此文章的示例。You can view this article’s sample on GitHub.
使用 fluent API 配置模型Use fluent API to configure a model
可在派生上下文中替代 OnModelCreating 方法,并使用 ModelBuilder API 来配置模型。You can override the OnModelCreating method in your derived context and use the ModelBuilder API to configure your model. 此配置方法最为有效,并可在不修改实体类的情况下指定配置。This is the most powerful method of configuration and allows configuration to be specified without modifying your entity classes. Fluent API 配置具有最高优先级,并将替代约定和数据注释。Fluent API configuration has the highest precedence and will override conventions and data annotations.
using Microsoft.EntityFrameworkCore;
namespace EFModeling.FluentAPI.Required
{
class MyContext : DbContext
{
public DbSet Blogs { get; set; }
#region Required
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity()
.Property(b => b.Url)
.IsRequired();
}
#endregion
}
public class Blog
{
public int BlogId { get; set; }
public string Url { get; set; }
}
}
分组配置Grouping configuration
To reduce the size of the OnModelCreating method all configuration for an entity type can be extracted to a separate class implementing IEntityTypeConfiguration.
public class BlogEntityTypeConfiguration : IEntityTypeConfiguration
{
public void Configure(EntityTypeBuilder builder)
{
builder
.Property(b => b.Url)
.IsRequired();
}
}
然后,只需从 OnModelCreating 调用 Configure 方法。Then just invoke the Configure method from OnModelCreating.
new BlogEntityTypeConfiguration().Configure(modelBuilder.Entity());
可以在给定程序集中应用实现 IEntityTypeConfiguration 的类型中指定的所有配置。It is possible to apply all configuration specified in types implementing IEntityTypeConfiguration in a given assembly.
modelBuilder.ApplyConfigurationsFromAssembly(typeof(BlogEntityTypeConfiguration).Assembly);
备注
应用配置的顺序是不确定的,因此仅当顺序不重要时才应使用此方法。The order in which the configurations will be applied is undefined, therefore this method should only be used when the order doesn't matter.
使用数据注释来配置模型Use data annotations to configure a model
也可将特性(称为数据注释)应用于类和属性。You can also apply attributes (known as Data Annotations) to your classes and properties. 数据注释会替代约定,但会被 Fluent API 配置替代。Data annotations will override conventions, but will be overridden by Fluent API configuration.
using Microsoft.EntityFrameworkCore;
using System.ComponentModel.DataAnnotations;
namespace EFModeling.DataAnnotations.Required
{
class MyContext : DbContext
{
public DbSet Blogs { get; set; }
}
#region Required
public class Blog
{
public int BlogId { get; set; }
[Required]
public string Url { get; set; }
}
#endregion
}