首页 > 其他 > 详细

EntityFramework Core笔记:表结构及数据操作(2)

时间:2018-05-27 17:10:10      阅读:308      评论:0      收藏:0      [点我收藏+]

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)

原文:https://www.cnblogs.com/libingql/p/9096503.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!