In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
这篇文章主要介绍"ASP.NET Core怎么使用EF创建模型",在日常操作中,相信很多人在ASP.NET Core怎么使用EF创建模型问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答"ASP.NET Core怎么使用EF创建模型"的疑惑有所帮助!接下来,请跟着小编一起来学习吧!
1.什么是Fluent API?
EF中内嵌的约定将POCO类映射到表。但是,有时您无法或不想遵守这些约定,需要将实体映射到约定指示外的其他对象,所以Fluent API和注解都是一种方法,这两种方法是用来配置EF在映射属性时绕开约定。Code first fluent API最常访问通过重写OnModelCreating方法在派生DbContext。
2.包含属性和排除属性
按照约定,数据模型中都包含一个getter和一个setter公共属性。
2.1包含属性
包含属性官网解释有点难以理解,我个人认为在OnModelCreating方法配置包含Blog模型,那么当我们调用Blog模型读写数据时候就会从连接数据库中读写对应Blog表。
protected override void OnModelCreating(ModelBuilder modelBuilder){ modelBuilder.Entity();}2.2排除属性
如果你不想往BlogMetadata上读写数据,可以使用数据批注或者fluent API从模型中排除该实体类型。
2.2.1数据批注namespace EFModeling.DataAnnotations.IgnoreType{ class MyContext : DbContext { public DbSet Blogs { get; set; } } public class Blog { public int BlogId { get; set; } public string Url { get; set; } public BlogMetadata Metadata { get; set; }} //读写不映射该实体 [NotMapped] public class BlogMetadata { public DateTime LoadedFromDatabase { get; set; } }}2.2.2Fluent APInamespace EFModeling.FluentAPI.IgnoreType{ class MyContext : DbContext { public DbSet Blogs { get; set; } protected override void OnModelCreating(ModelBuilder modelBuilder) { //Ignore方法就是读写不映射该实体 modelBuilder.Ignore(); } } public class Blog { public int BlogId { get; set; } public string Url { get; set; } public BlogMetadata Metadata { get; set; } } public class BlogMetadata { public DateTime LoadedFromDatabase { get; set; } }}3.主键
使用关系型数据库时候,都会涉及到主键概念,用作每个实体实例的主要唯一标识符。
3.1数据批注namespace EFModeling.DataAnnotations.KeySingle{ class MyContext : DbContext { public DbSet Cars { get; set; } } class Car { //设置LicensePlate为主键 [Key] public string LicensePlate { get; set; } public string Make { get; set; } public string Model { get; set; } }}3.2Fluent APInamespace EFModeling.FluentAPI.KeySingle{ class MyContext : DbContext { public DbSet Cars { get; set; } protected override void OnModelCreating(ModelBuilder modelBuilder) { modelBuilder.Entity() //设置LicensePlate为主键 .HasKey(c => c.LicensePlate); } } class Car { public string LicensePlate { get; set; } public string Make { get; set; } public string Model { get; set; } }}4.生成值
有三个可用于属性的值生成模式:
●无值生成:没有值生成意味着,需始终提供要保存到数据库的有效值。必须先将有效的值赋予新的实体,再将这些新的实体添加到上下文中。
●在添加时生成值:在添加时生成值,意思是为新实体生成值。
●在添加或更新时生成值:在添加或更新时生成值,意味着在每次保存该记录(插入或更新)时生成新值。
注:如果想在数据库端添加或更新时自动生成值,我们可以通过触发器和配置默认值等方法生成。例如,如果指定在添加或更新时要生成DateTime属性,则必须设置生成值的方法。若要执行此操作,一种方法是配置GETDATE() 的默认值以生成新行的值,然后即可使用数据库触发器在更新过程中生成值,如下面的示例触发器所示:
USE [Blogging]GO/****** Object: Trigger [dbo].[Blog_Update_Trigger] Script Date: 2019/10/22 16:18:13 ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOALTER TRIGGER [dbo].[Blog_Update_Trigger] ON [dbo].[Blog] AFTER UPDATEASBEGIN SET NOCOUNT ON; IF ((SELECT TRIGGER_NESTLEVEL()) > 1) RETURN;DECLARE @Id INT SELECT @Id = INSERTED.BlogId FROM INSERTED UPDATE dbo.Blog SET Updatetime = GETDATE() WHERE BlogId = @IdEND4.1数据批注4.1.1无值生成public class Blog{ [DatabaseGenerated(DatabaseGeneratedOption.None)] public int BlogId { get; set; } public string Url { get; set; }}4.1.2在添加时生成值public class Blog{ public int BlogId { get; set; } public string Url { get; set; } [DatabaseGenerated(DatabaseGeneratedOption.Identity)] public DateTime Inserted { get; set; }}4.1.3在添加或更新时生成值public class Blog{ public int BlogId { get; set; } public string Url { get; set; } [DatabaseGenerated(DatabaseGeneratedOption.Computed)] public DateTime LastUpdated { get; set; }}4.2Fluent API4.2.1无值生成modelBuilder.Entity() .Property(b => b.BlogId) .ValueGeneratedNever();4.2.2在添加时生成值modelBuilder.Entity() .Property(b => b.Inserted).ValueGeneratedOnAdd();4.2.3在添加或更新时生成值modelBuilder.Entity() .Property(b => b.LastUpdated) .ValueGeneratedOnAddOrUpdate();到此,关于"ASP.NET Core怎么使用EF创建模型"的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注网站,小编会继续努力为大家带来更多实用的文章!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.