EntityFramWorkCore连接MySql

一、添加库:

 

 二、添加实体类和数据库上下文:

 当类名和表名一样可以不需要添加Table属性

 

public class AppDbContext:DbContext
    {
        public AppDbContext(DbContextOptions<AppDbContext> options) :base(options)
        {

        }
        public DbSet<PropertyItem> PropertyItems { get; set; }
    }

三、添加服务依赖:

services.AddDbContext<AppDbContext>(options => options.UseMySql(Configuration.GetConnectionString("conn"),MySqlServerVersion.LatestSupportedServerVersion));

图中conn为数据库连接对象,在appsetting.json中:

 

四、操作数据:

通过依赖注入并使用仓储模式实现控制反转:

 

 

posted @ 2021-12-17 14:28  点终将连成线  阅读(225)  评论(0编辑  收藏  举报