EFCore_创建项目

添加依赖

Microsoft.EntityFrameworkCore
Microsoft.EntityFrameworkCore.Tools(Migration工具)

根据使用的DB添加对应依赖:

SQL Server:Microsoft.EntityFrameworkCore.SqlServer
添加该依赖时可不添加Microsoft.EntityFrameworkCore,该依赖会捆绑添加

MySQL: Pomelo.EntityFrameworkCore.MySQL
该依赖为民间项目。Oracle官方也提供MySQL依赖,但是跟新速度极慢

......

创建实体类

internal class Book
{
    public long Id { get; set; }
    public string Title { get; set; }
    public string AuthorName { get; set; }
    public DateTime PubTime { get; set; }
    public double Price { get; set; }
}

创建配置类

1. 实体类配置类(可选)

internal class BookConfig : IEntityTypeConfiguration<Book>
{
    public void Configure(EntityTypeBuilder<Book> builder)
    {
        builder.ToTable("Books"); // 表名
        builder.Property(e => e.Title) // 对属性进行设置
            //.HasColumnName("Book_Title")     // 字段名(默认为属性名)
            //.HasColumnType("varchar(50)")    // 字段类型
            //.HasDefaultValue("DefaultTitle") // 字段默认值
            .IsRequired(true)  // 字段是否可为null
            .HasMaxLength(50); // 字段最大长度
        //builder.Ignore(e => e.PubTime); // 忽略字段(不进行实体类->表的映射)
        //builder.HasKey(e => e.Id);      // 设置主键
        //builder.HasIndex(e => e.Title); // 设置索引
        //builder.HasIndex(e => new { e.Title, e.AuthorName }); // 设置复合索引 | IsUnique()设置唯一索引 | IsClustered()设置聚集索引
    }
}

配置类实现IEntityTypeConfiguration<>接口,可在其中对表进行详细设置,常用可设置项详见代码注释(若不创建实体类配置类,则会采用默认配置,详见本节文末)

2. DB配置类

internal class ThisDbContext : DbContext
{
    public DbSet<Book> Books { get; set; } // 实体
    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        string connStr = "server=localhost;port=3306;database=x;user=root;password=xyz";
        //optionsBuilder.UseSqlServer(connStr); // SQLServer连接设置
        optionsBuilder.UseMySql(connStr, new MySqlServerVersion(new Version(8, 0, 33))); // MySQL连接设置
    }
    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);
        modelBuilder.ApplyConfigurationsFromAssembly(this.GetType().Assembly);
        // 实体类配置类(实现IEntityTypeConfiguration<>接口的类)所属Assembly
        // GetType()等价于typeof(OneToManyDbContext), 即typeof(当前类)
    }
}

默认配置:

1.默认将实体类属性名作为表字段名
2.默认将DbSet<>属性名作为表名
3.默认将"Id"或"实体类名+Id"属性对应的表字段设置为主键

初始化DB

在"程序包管理器控制台"中执行:
add-migration xxx(此次migration的代号)
update-database

关于常用命令,可参考 EFCore_Migration常用命令-CSDN博客

试运行

DB:

Program.cs

internal class Program
{
    static void Main(string[] args)
    {
        using ThisDbContext dbContext = new();
        
        IQueryable<Book> books = dbContext.Books.Where(e => e.Price > 0);
        foreach (var item in books)
        {
            Console.WriteLine(item.Title);
        }
    }
}

运行结果

  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值