Esempio n. 1
0
 /// <summary>
 /// 根据主键Id获取一个用户
 /// </summary>
 /// <param name="id">主键Id</param>
 /// <param name="connectionString">链接字符串</param>
 /// <returns></returns>
 public User RetriveOneEntityById(int id, string connectionString = null)
 {
     using (LightContext context = MySQLDataBaseConfig.CreateContext(connectionString))
     {
         return(context.Find <User>(id));
     }
 }
Esempio n. 2
0
 /// <summary>
 /// 修改一个用户
 /// </summary>
 /// <param name="entity">要修改的用户</param>
 /// <param name="connectionString">链接字符串</param>
 /// <returns></returns>
 public bool UpdateEntity(User entity, string connectionString = null)
 {
     using (LightContext context = MySQLDataBaseConfig.CreateContext(connectionString))
     {
         context.Update <User>(entity);
         return(context.SaveChanges() > 0);
     }
 }
Esempio n. 3
0
 /// <summary>
 /// 根据主键Id删除一个用户
 /// </summary>
 /// <param name="id">主键Id</param>
 /// <param name="connectionString">链接字符串</param>
 /// <returns></returns>
 public bool DeleteEntityById(int id, string connectionString = null)
 {
     using (LightContext context = MySQLDataBaseConfig.CreateContext(connectionString))
     {
         context.User.Remove(context.Find <User>(id));
         return(context.SaveChanges() > 0);
     }
 }
Esempio n. 4
0
 /// <summary>
 /// 批量添加实体
 /// </summary>
 /// <param name="entityList">要创建的实体</param>
 /// <param name="connectionString">链接字符串</param>
 /// <returns></returns>
 public bool CreateEntityList(IEnumerable <User> entityList, string connectionString = null)
 {
     using (LightContext context = MySQLDataBaseConfig.CreateContext(connectionString))
     {
         context.User.AddRange(entityList);
         return(context.SaveChanges() > 0);
     }
 }
Esempio n. 5
0
 /// <summary>
 /// 获取所有用户
 /// </summary>
 /// <param name="connectionString">链接字符串</param>
 /// <returns></returns>
 public IEnumerable <User> RetriveAllEntity(string connectionString = null)
 {
     using (LightContext context = MySQLDataBaseConfig.CreateContext(connectionString))
     {
         List <User> allUsers = new List <User>();
         allUsers.AddRange(context.User);
         return(allUsers);
     }
 }
Esempio n. 6
0
        public static LightContext CreateContext(string mySqlConnectionString = null)
        {
            if (string.IsNullOrWhiteSpace(mySqlConnectionString))
            {
                mySqlConnectionString = DefaultMySqlConnectionString;
            }
            var optionBuilder = new DbContextOptionsBuilder <LightContext>();

            //var migrationsAssembly = typeof(Startup).GetTypeInfo().Assembly.GetName().Name;
            optionBuilder.UseMySQL(mySqlConnectionString, m =>
            {
            });
            var context = new LightContext(optionBuilder.Options);

            context.Database.EnsureCreated();
            return(context);
        }