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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

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

發布時間:2023/12/10 编程问答 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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 实体配置_创建并配置模型的全部內容,希望文章能夠幫你解決所遇到的問題。

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

主站蜘蛛池模板: 色窝窝无码一区二区三区成人网站 | 日韩精品一级 | 日产精品久久久一区二区 | 性欧美精品| 成人网在线免费观看 | 国内特级毛片 | 绯色av蜜臀vs少妇 | 久久精品福利视频 | 国产性自拍 | 少妇久久久久久被弄到高潮 | 日韩美女视频网站 | youjizz自拍 | 成人国产精品免费 | 欧美日韩一区二区三区在线播放 | 国产不卡在线 | 久久久.com | 中文 日韩 欧美 | 久久精品国产亚洲av麻豆色欲 | 91免费国产在线 | 欧美黄色精品 | 美女精品久久 | jizz在线播放 | 国产免费一级片 | 久久久精品电影 | 俺去俺来也在线www色官网 | 国产精品色婷婷 | 国产欧美一区二区三区鸳鸯浴 | 精品国产乱码久久久久久牛牛 | 日日摸日日碰 | 疯狂做爰的爽文多肉小说王爷 | 亚洲欧美日韩一区在线观看 | 黄色永久免费网站 | 黄色成人在线观看 | 国产99对白在线播放 | 波多野结衣家庭主妇 | 欧美淫 | 深夜精品视频 | 美女扒开内裤让男人捅 | 潘金莲激情呻吟欲求不满视频 | 成年人av| 法国空姐在线观看免费 | 在线免费观看黄视频 | 日本一二三区视频在线 | 国产在线www | a色视频| 国产寡妇亲子伦一区二区三区四区 | www.av天天 | 99久久99久久 | 日日爱夜夜操 | 欧美国产日韩精品 | 91免费看网站 | 国外av在线 | 男人女人拔萝卜视频 | 久久久久久91香蕉国产 | 日韩视频中文字幕在线观看 | 中文字幕日本一区二区 | 操欧美美女 | 亚洲高清视频网站 | 精品国产99| 亚洲综合久 | 亚洲婷婷一区 | 97人妻精品一区二区三区视频 | 日韩人妻无码一区二区三区99 | 亚洲m码 欧洲s码sss222 | 成人午夜淫片100集 伊人久久国产 | 国产女主播一区 | 成人福利av | 无码国产精品一区二区免费16 | 黄色美女av | 日本免费一区二区三区 | 精品无码国产一区二区三区av | 揄拍成人国产精品视频 | 四虎永久在线视频 | 91高跟黑色丝袜呻吟在线观看 | 最新免费av网站 | 国产高潮av | 免费欧美黄色片 | 五月天综合社区 | 91网站免费 | 中文字幕 欧美激情 | 日本少妇激情舌吻 | 人人草人人搞 | 欧美日韩人妻精品一区 | 久久久久久久久久久久久久免费看 | 国产激情av | 天天毛片 | 久久国产精品免费 | wwwa级片 | 少妇太爽了太深了太硬了 | 美女扒开腿免费视频 | 成人看片网站 | 俺来也av| 高跟肉丝丝袜呻吟啪啪网站av | 99热这里只有精 | 亚洲福利网 | a∨视频 | 日本黄网站 | 欧美日批视频 | 亚洲精品一区二区潘金莲 |