当前位置: 代码网 > it编程>编程语言>Asp.net > .Net使用EF Core框架连接Oracle的方法

.Net使用EF Core框架连接Oracle的方法

2025年04月24日 Asp.net 我要评论
一、nutget包添加一定要安装oracle.mangeddataaccess,他是oracle的驱动程序。如果是使entityframeworkcore框架操作oracle,则还需要引入oracle

一、nutget包添加

一定要安装oracle.mangeddataaccess,他是oracle的驱动程序。如果是使entityframeworkcore框架操作oracle,则还需要引入oracle.entityframeworkcore

二、 配置文件

在appsetting.json文件中写好数据的连接信息,这里我本来使用的是data source=tns别名,但是报错了,后面我也看了tnsnames.ora中的信息,tns别名也没有错。但是一直报错,所以我只能将这个连接信息全部写全了。

{
  "connectionstrings": {
    "oracleconnect": "user id=用户名;password=用户密码;data source=(description=(address=(protocol=tcp)(host=127.0.0.1)(port=1521))(connect_data=(service_name=服务名称)))"
  },
  "logging": {
    "loglevel": {
      "default": "information",
      "microsoft.aspnetcore": "warning"
    }
  },
  "allowedhosts": "*"
}

三、创建实体类

因为oracle默认的话不管是大写还是小写,最后都会转换成大写,所以如果实体类的名字如果和表明不一致的话(大写小写oracle是有区别的),会报错!

所以使用了[table]属性来映射对应的表。

包括属性名也是如此,使用了[column]来映射对应的属性名

using system.componentmodel.dataannotations.schema;
namespace oracleconnecttest.models
{
    [table("people")]
    public class people
    {
        //oracle默认全部是大写
        [column("id")]
        public int id { get; set; }
        [column("name")]
        public string name { get; set; }
    }
}

四、创建数据库上下文类

创建一个data文件夹,将该类(数据库上下文类)放入其中(个人习惯!)

using microsoft.entityframeworkcore;
using oracleconnecttest.models;
using system.componentmodel.dataannotations.schema;
namespace oracleconnecttest.data
{
    public class oracleconnecttestcontext : dbcontext
    {
        public oracleconnecttestcontext(dbcontextoptions options) : base(options)
        {
        }
        public dbset<people> peoples { get; set; }
    }
}

五、将数据库上下文服务注册到容器

program.cs

using microsoft.entityframeworkcore;
using oracleconnecttest.data;
var builder = webapplication.createbuilder(args);
//依赖项注入
builder.services.adddbcontext<oracleconnecttestcontext>(options =>
         options.useoracle(builder.configuration.getconnectionstring("oracleconnect")));

六、测试数据库数据

(1)编写peoplecontroller

using microsoft.aspnetcore.mvc;
using oracleconnecttest.data;
namespace oracleconnecttest.controllers
{
    public class peoplecontroller : controller
    {
        private readonly oracleconnecttestcontext _context;
        public peoplecontroller(oracleconnecttestcontext context)
        {
            _context = context;
        }
        public iactionresult index()
        {
            var s=_context.peoples.tolist();
            return view(s);
        }
    }
}

(2)编写people页面

@*
    for more information on enabling mvc for empty projects, visit https://go.microsoft.com/fwlink/?linkid=397860
*@
@{
}
@model list<oracleconnecttest.models.people>;
@foreach(var item in model){
    <p>@item.id</p>
    <p>@item.name</p>
}

到此这篇关于.net使用ef core框架如何连接oracle的文章就介绍到这了,更多相关.net ef core连接oracle内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com