EntityFramwork 链接各类型数据库

1、EF 链接各类型数据库

1.1、Core EF 链接SQL Server
Scaffold-DbContext "data source=cnwuxg0te01;initial catalog=dbName;persist security info=True;user id=sa;password=Jabil12345;MultipleActiveResultSets=True;App=EntityFramework" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models -Tables Employee
1.2、.net EF 链接postgreSql

选择 .net 4.6.1
1、安装插件 Npgsql PostgreSQL Integration
2、安装 EF框架 无框架限制
3、安装 Npgsql 注意.net版本
4、安装 EF6 for Npgsql 提供postgresql的ADO模型,要匹配:.net 以及 2,3 的版本

1.3、Core EF 链接postgreSql

1、安装插件 Npgsql PostgreSQL Integration
2、安装 EF框架:Microsoft.EntityFrameworkCore
3、Microsoft.EntityFrameworkCore.Design;
Microsoft.EntityFrameworkCore.Tools
4、安装 Npgsql: Npgsql.EntityFrameworkCore.PostgreSQL
5、Npgsql.EntityFrameworkCore.PostgreSQL.Design
6、cmd->cd 进入工程文件.csproj所在的文件夹 ->
shell dotnet ef dbcontext scaffold "Host=cnwuxm1medb01;Database=EC;Username=ECUser;Password=Jabil123" Npgsql.EntityFrameworkCore.PostgreSQL -o Models

  • Use dotnet build to see the errors

    程序编译出错了,要保证程序能够正常运行的基础上,更新数据库cs文件

  • Use the Force flag to overwrite these files

    dotnet ef dbcontext scaffold "Host=cnwuxm1medb01;Database=FOF;Username=FOFUser;Password=Jabil123" Npgsql.EntityFrameworkCore.PostgreSQL -o Model -f

1.4、Core EF 链接MySQL

1.安装 EF框架:Microsoft.EntityFrameworkCore
2.Microsoft.EntityFrameworkCore.Tools
3.安装 Mysql.Data
4.安装 Pomelo.EntityFrameworkCore.MySql(官方插件有问题,使用这个社区版本)
5.cmd->cd 进入工程文件.csproj所在的文件夹 ->
Scaffold-DbContext "server=cnwuxg0te01;uid=root;pwd=Jabil12345;port=3306;database=sparepart_current;" Pomelo.EntityFrameworkCore.MySql -OutputDir Models -f

2、查询语句

可以使用linq语句,或者EF结构

动态条件查询
var item = db.pmsheets.ToList();
    if (!string.IsNullOrEmpty(department))
        item = item.Where(e => e.department == department).ToList();
    if (!string.IsNullOrEmpty(project))
        item = item.Where(e => e.project == project).ToList();
    var items = item.Select(e => new {
        paras = e.station
    }).Distinct().ToList();
两表链接
var pmitems = db.PMItems
        .Join(db.EQs, pm => pm.EQID, eq => eq.EQID, (pm, eq) => new { 
            pmid=pm.pmid,
            EQID=pm.pmid,
            owner = eq.Owners,
        })
        .Where(e => pmid.Contains(e.pmid)).ToList();
Where 不执行
pmms pmmsdb = new pmms();
inspect db = new inspect();
var eqwhere = pmmsdb.EQs.Where(e => true);
if (department.Length>0) eqwhere = eqwhere.Where(e => department.Contains(e.Department));
if (line.Length > 0) eqwhere = eqwhere.Where(e => line.Contains(e.Line));
if (project.Length > 0) eqwhere = eqwhere.Where(e => project.Contains(e.Workcell));
var eqlist = eqwhere.Select(e => new {
    Line = e.Line,
    EQID = e.EQID
}).ToList();
EF 实现sql中的方法
using Microsoft.EntityFrameworkCore;
EF.Functions
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值