zoukankan      html  css  js  c++  java
  • CodeFirst简单演示的步骤

    CodeFirst简单演示的步骤

    1. 创建实体类【Student】

    public class Student

        {

            public long Id { get; set; }

            public string Name { get; set; }

            public string Address { get; set; }

            public DateTime CreateTime { get; set; }

            public short IsDelete { get; set; }

        }

    1. 创建数据库【名称为CodeFirstDB】
    2. 配置连接字符串

    <!--数据库配置-->

        <add name="consrt" connectionString="server=.;database=CodeFirstDB;uid=sa;pwd=123456" providerName="System.Data.SqlClient"/>

    1. 创建数据库上下文类

    public class MyDbContext : DbContext

        {

            public MyDbContext()

                : base("name=consrt")

            {

            }

            /// <summary>

            /// 创建数据库的策略

            /// </summary>

            /// <param name="modelBuilder"></param>

            protected override void OnModelCreating(DbModelBuilder modelBuilder)

            {

                base.OnModelCreating(modelBuilder);

            }

            public DbSet<Student> Students { get; set; }

        }

    1. 实体类的配置文件【在项目中创建文件夹ModelConfig,在里面添加实体类的 配置信息【FluentAPI】】

    public class StudentConfig : EntityTypeConfiguration<Student>

        {

            public StudentConfig()

            {

                this.ToTable("Students");

                this.Property(p => p.Name)

                    .HasMaxLength(30)//最大长度

                    .IsRequired()//不允许为空

                    .IsUnicode(false);// 是 varchar

                this.Property(p => p.Address)

                    .HasMaxLength(100)//最大长度

                    .IsOptional()//允许为空

                    .IsUnicode(true)//是 n

                    .IsFixedLength();//固定长度 nchar(100)

              

            }

        }

  • 相关阅读:
    LoadRunne遇到的一些问题FAQ(持续更新...)
    LoadRunner11下载、安装与破解
    LoadRunner之录制你的第一个脚本
    appium+Linux环境安装配置
    appium-FAQ(持续更新...)
    appium启动运行log分析
    利用Unity3D与Oculus实现机器情绪安抚师的一种方案
    利用Unity3D实现多平台增强现实网络游戏的一种方案
    Ubuntu16.04安装NVIDIA驱动时的一些坑与解决方案
    你的计算机也可以看懂世界——十分钟跑起卷积神经网络(Windows+CPU)
  • 原文地址:https://www.cnblogs.com/Learnblog/p/9993208.html
Copyright © 2011-2022 走看看