日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

efcore 实体配置_创建并配置模型

發布時間:2023/12/10 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 efcore 实体配置_创建并配置模型 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

創建并配置模型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

}

總結

以上是生活随笔為你收集整理的efcore 实体配置_创建并配置模型的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。