Data Annotations:
using System.ComponentModel.DataAnnotations.Schema;
[Table("Role")] public class Role { // ... }
FluentAPI:
using Microsoft.EntityFrameworkCore;
protected override void OnModelCreating(ModelBuilder modelBuilder) { modelBuilder.Entity<Role>().ToTable("Role"); }
Data Annotations:
using System; using System.Collections.Generic; using System.Text; using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema; namespace Libing.App.Models.Entities { [Table("Role")] public class Role { [Column("RoleID")] public int RoleID { get; set; } [Required] [Column("RoleName",TypeName = "varchar(200)")] public string RoleName { get; set; } } }
FluentAPI:
protected override void OnModelCreating(ModelBuilder modelBuilder) { modelBuilder.Entity<Role>().ToTable("Role"); modelBuilder.Entity<Role>() .Property(t => t.RoleID) .HasColumnName("RoleID"); modelBuilder.Entity<Role>() .Property(t => t.RoleName) .HasColumnName("RoleName") .HasColumnType("varchar(200)") //.HasMaxLength(200) .IsRequired(); }
EntityFramework Core笔记:表结构及数据操作(2)
原文:https://www.cnblogs.com/libingql/p/9096503.html