.net core EF 关系(二) 私有字段
PropertyAccessMode 枚举 (Microsoft.EntityFrameworkCore) | Microsoft Learn
支持字段 - EF Core | Microsoft Learn
ef 是可以对私有字段配置的
/// <summary> /// 博客 /// </summary> public class Blog { public Blog() { Posts = new List<Post>(); } public int Id { get; set; } /// <summary> /// 导航属性发布集合 /// </summary> public IEnumerable<Post> Posts { get; set; } /// <summary> /// 私有字段 /// </summary> private string _url; /// <summary> /// 属性 /// </summary> public string Url =>_url; /// <summary> /// 私有字段 /// </summary> private string soltCode; } /// <summary> /// 发布 /// </summary> public class Post { public int Id { get; set; } /// <summary> /// 外键 博客Id /// </summary> public int BlogId { get; set; } /// <summary> /// 导航属性Blog /// </summary> public Blog? Blog { get; set; } }
配置1 | builder.Property(x => x.Url).HasField("_url"); |
配置2 属性对私有字段可读可写 | builder.Property(x=>x.Url).HasField("_url").UsePropertyAccessMode(PropertyAccessMode.Field); |
配置3 只有私有字段 | builder.Property("soltCode").HasColumnName("SoltCode"); |
public class BlogEntityTypeConfiguration : IEntityTypeConfiguration<Blog> { public void Configure(EntityTypeBuilder<Blog> builder) { builder.ToTable("T_Blogs"); builder.HasKey(x => x.Id); //博客和发布时一对多 导航属性 Blog 不为null 外键博客BlogId 必须存在关系 IsRequired() 默认为true //builder.HasMany(x=>x.Posts).WithOne(x=>x.Blog).HasForeignKey(x=>x.BlogId).IsRequired(); //博客和发布时一对多 导航属性 Blog 为null 外键为null 博客BlogId 必须存在关系 IsRequired(false) //builder.HasMany(x=>x.Posts).WithOne(x=>x.Blog).HasForeignKey(x=>x.Id).IsRequired(false); //博客和发布时一对多 只有导航属性 Blog 没有外键 的必须存在关系IsRequired() // builder.HasMany(x => x.Posts).WithOne(x => x.Blog).HasForeignKey("BlogId").IsRequired(); //博客和发布时一对多 只有导航属性 Blog 没有外键 的必须存在关系IsRequired(false) // builder.HasMany(x => x.Posts).WithOne(x => x.Blog).HasForeignKey("BlogId").IsRequired(false); //博客和发布时一对多 Blog有导航属性 Post没有有导航属性 Blog 只有有外键 的必须存在关系IsRequired() // builder.HasMany(x => x.Posts).WithOne().HasForeignKey(x=>x.BlogId).IsRequired(); //博客和发布时一对多 Blog没有有导航属性 只有有外键 的必须存在关系IsRequired() builder.HasMany<Post>().WithOne().HasForeignKey(x => x.BlogId).IsRequired(); //私有字段 //builder.Property(x => x.Url).HasField("_url"); builder.Property(x=>x.Url).HasField("_url").UsePropertyAccessMode(PropertyAccessMode.Field); builder.Property("soltCode").HasColumnName("SoltCode"); } }
迁移文件
/// <inheritdoc /> public partial class init1 : Migration { /// <inheritdoc /> protected override void Up(MigrationBuilder migrationBuilder) { migrationBuilder.AlterDatabase() .Annotation("MySql:CharSet", "utf8mb4"); migrationBuilder.CreateTable( name: "T_Blogs", columns: table => new { Id = table.Column<int>(type: "int", nullable: false) .Annotation("MySql:ValueGenerationStrategy", MySqlValueGenerationStrategy.IdentityColumn), Url = table.Column<string>(type: "longtext", nullable: false) .Annotation("MySql:CharSet", "utf8mb4"), SoltCode = table.Column<string>(type: "longtext", nullable: false) .Annotation("MySql:CharSet", "utf8mb4") }, constraints: table => { table.PrimaryKey("PK_T_Blogs", x => x.Id); }) .Annotation("MySql:CharSet", "utf8mb4"); migrationBuilder.CreateTable( name: "Post", columns: table => new { Id = table.Column<int>(type: "int", nullable: false) .Annotation("MySql:ValueGenerationStrategy", MySqlValueGenerationStrategy.IdentityColumn), BlogId = table.Column<int>(type: "int", nullable: false), BlogId1 = table.Column<int>(type: "int", nullable: true) }, constraints: table => { table.PrimaryKey("PK_Post", x => x.Id); table.ForeignKey( name: "FK_Post_T_Blogs_BlogId", column: x => x.BlogId, principalTable: "T_Blogs", principalColumn: "Id", onDelete: ReferentialAction.Cascade); table.ForeignKey( name: "FK_Post_T_Blogs_BlogId1", column: x => x.BlogId1, principalTable: "T_Blogs", principalColumn: "Id"); }) .Annotation("MySql:CharSet", "utf8mb4"); migrationBuilder.CreateIndex( name: "IX_Post_BlogId", table: "Post", column: "BlogId"); migrationBuilder.CreateIndex( name: "IX_Post_BlogId1", table: "Post", column: "BlogId1"); } /// <inheritdoc /> protected override void Down(MigrationBuilder migrationBuilder) { migrationBuilder.DropTable( name: "Post"); migrationBuilder.DropTable( name: "T_Blogs"); } }