EntityFramework.Extended 支持 mysql
【摘要】
1.安装EntityFramework.Extended 或者 Z.EntityFramework.Plus.EF6(推荐)
2.更改MyDBContext配置
namespace EntityFrameworkCore{ [DbConfigurationType(...
1.安装EntityFramework.Extended 或者 Z.EntityFramework.Plus.EF6(推荐)
2.更改MyDBContext配置
-
namespace EntityFrameworkCore
-
{
-
[DbConfigurationType(typeof(MySql.Data.Entity.MySqlEFConfiguration))]
-
public class MyDBContext : DbContext
-
{
-
public MyDBContext()
-
:base("name=Default")
-
{
-
//新增配置
-
Database.SetInitializer<MyDBContext>(null);
-
}
-
-
//新增配置(Z.EntityFramework.Plus.EF6不需要)
-
protected override void OnModelCreating(DbModelBuilder modelBuilder)
-
{
-
modelBuilder.HasDefaultSchema("");
-
base.OnModelCreating(modelBuilder);
-
}
-
-
public DbSet<User> User { set; get; }
-
-
//...................
-
}
-
}
3.Global.asax中注册扩展
-
using System;
-
using System.Collections.Generic;
-
using System.Data.Entity;
-
using System.Linq;
-
using System.Web;
-
using System.Web.Http;
-
using System.Web.Mvc;
-
using System.Web.Optimization;
-
using System.Web.Routing;
-
-
namespace YFAPICommon
-
{
-
public class WebApiApplication : System.Web.HttpApplication
-
{
-
protected void Application_Start()
-
{
-
AreaRegistration.RegisterAllAreas();
-
GlobalConfiguration.Configure(WebApiConfig.Register);
-
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
-
RouteConfig.RegisterRoutes(RouteTable.Routes);
-
BundleConfig.RegisterBundles(BundleTable.Bundles);
-
-
EfExtendedRegister();
-
}
-
-
//注册扩展(Z.EntityFramework.Plus.EF6不需要)
-
private void EfExtendedRegister()
-
{
-
var container = new EntityFramework.Container();
-
EntityFramework.Locator.RegisterDefaults(container);
-
container.Register<EntityFramework.Batch.IBatchRunner>(() => new EntityFramework.Batch.MySqlBatchRunner());
-
EntityFramework.Locator.SetContainer(container);
-
}
-
}
-
}
4.使用
-
//注:Delete和Update之后不用再dbContext.SaveChanges();
-
-
dbContext.User.Where(u => u.id == 1).Delete();
-
-
dbContext.User.Where(u => u.id == 1).Update(u=>new User { nickName="my name"});
-
//Z.EntityFramework.Plus.EF6
-
public object future()
-
{
-
//三个值在一次SQL查询中获得,提高查询效率
-
var query = dbContext.User.Future();
-
var datalist = query.ToList();
-
int count = query.Count();
-
var data = query.FirstOrDefault();
-
return new { datalist, count, data };
-
}
-
//FromCache
-
QueryCacheManager.ExpireAll();//清空缓存
-
dbContext.User.FromCache().ToList();//从缓存读取
文档:
https://github.com/zzzprojects/EntityFramework.Extended
https://entityframework-plus.net/?z=ef-extended
文章来源: zzzili.blog.csdn.net,作者:清雨小竹,版权归原作者所有,如需转载,请联系作者。
原文链接:zzzili.blog.csdn.net/article/details/119783080
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)