MySQL 存储引擎

存储引擎

MySQL5.5以后默认使用InnoDB存储引擎

1、MyISAM:它不支持事务,也不支持外键,尤其是访问速度快,对事务完整性没有要求或者以SELECT、INSERT为主的应用基本都可以使用这个引擎来创建表。
2、InnoDB:InnoDB存储引擎提供了具有提交、回滚和崩溃恢复能力的事务安全。但是对比MyISAM的存储引擎,InnoDB写的处理效率差一些并且会占用更多的磁盘空间以保留数据和索引。


修改数据库表引擎模式:


ALTER TABLE 表名 ENGINE = InnoDB;


C# Entity Framework 代码中使用事物

1、添加引用System.Transactions
2、using System.Transactions;


public bool CreatePost(Posts data, Dictionary<string, string> dicList)
{
    try
    {
        using (TransactionScope tran = new TransactionScope())
        {
            dbContent.PostService.Add(data);
            dbContent.SaveChanges();
            long id = data.ID;
            foreach (var item in dicList)
            {
                var model = new PostMeta
                {
                    post_id = id,
                    meta_key = item.Key,
                    meta_value = item.Value,
                };
                dbContent.PostMetaService.Add(model);
            }
            dbContent.SaveChanges();
            tran.Complete();
            return true;
        }
    }
    catch (Exception ex)
    {
        Log4netHelper.Tool.LogError("CreatePostNew()", ex.Message);
    }
    return false;
}




你可能感兴趣的:(MySQL 存储引擎)