1. 程式人生 > >EntityFramework Core筆記:表結構及數據操作(2)

EntityFramework Core筆記:表結構及數據操作(2)

IV totable prot table AS lec ext lib models

1. 表結構操作

1.1 表名

  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"); }

1.2 字段

  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(); }

1.3 主鍵

1.4 關系

1.5 索引

2. 表數據操作

EntityFramework Core筆記:表結構及數據操作(2)