efcore 实体配置_创建并配置模型
創(chuàng)建并配置模型Creating and configuring a model
10/13/2020
本文內(nèi)容
Entity Framework 使用一組約定基于實體類的形狀構(gòu)建模型。Entity Framework uses a set of conventions to build a model based on the shape of your entity classes. 可指定其他配置以補充和/或替代約定的內(nèi)容。You can specify additional configuration to supplement and/or override what was discovered by convention.
本文介紹可應(yīng)用于面向任何數(shù)據(jù)存儲的模型的配置,以及面向任意關(guān)系數(shù)據(jù)庫時可應(yīng)用的配置。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. 提供程序還可支持特定于具體數(shù)據(jù)存儲的配置。Providers may also enable configuration that is specific to a particular data store. 有關(guān)提供程序特定配置的文檔,請參閱數(shù)據(jù)庫提供程序部分。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 配置具有最高優(yōu)先級,并將替代約定和數(shù)據(jù)注釋。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 調(diào)用 Configure 方法。Then just invoke the Configure method from OnModelCreating.
new BlogEntityTypeConfiguration().Configure(modelBuilder.Entity());
可以在給定程序集中應(yīng)用實現(xiàn) IEntityTypeConfiguration 的類型中指定的所有配置。It is possible to apply all configuration specified in types implementing IEntityTypeConfiguration in a given assembly.
modelBuilder.ApplyConfigurationsFromAssembly(typeof(BlogEntityTypeConfiguration).Assembly);
備注
應(yīng)用配置的順序是不確定的,因此僅當(dāng)順序不重要時才應(yīng)使用此方法。The order in which the configurations will be applied is undefined, therefore this method should only be used when the order doesn't matter.
使用數(shù)據(jù)注釋來配置模型Use data annotations to configure a model
也可將特性(稱為數(shù)據(jù)注釋)應(yīng)用于類和屬性。You can also apply attributes (known as Data Annotations) to your classes and properties. 數(shù)據(jù)注釋會替代約定,但會被 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
}
總結(jié)
以上是生活随笔為你收集整理的efcore 实体配置_创建并配置模型的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: windows 7搭建流媒体服务
- 下一篇: 小米手环7 NFC版上手:无可挑剔的巨屏