简体   繁体   中英

Retrieving data from Oracle db in asp.net mvc using linq

I'm a newbie with less than a year of experience. Sadly I have nobody to ask in my company, so I'm spending a lot of time to find out what's wrong.

I just finished asp.net mvc project and try to change only DB
From: SQL Server
To: Oracle

In my original project, I retrieved data like this from SQL Server. Works well.

using (var haksaContext = new HaksaContext())
{
    try{
        var haksa = haksaContext.HaksaMembers.Where(m => m.stnt_numb == model.Stnt_Numb).ToList();
        if (haksa.Count == 1){

My code has no problem in SQL Server so I searched and installed Nuget package for Oracle, Oracle developer tools for VS 2015 and I successfully added Oracle DB to my Server Explorer. And target table in OracleDB has the same column name, structure.(actually there are some difference in data type like int <-> number)

If I query using Query Window, I can select from target table. Seems no problem here:

如果使用查询窗口查询,则可以从目标表中进行选择。在这里似乎没问题。

I copied original dbcontext(HaksaContext) to make OracleContext and modified it like next photo

public class HaksaContext : DbContext //-> OracleContext
    {
        public HaksaContext() : base("HaksaContext") { } //-> OracleContext
        public DbSet<UniversityMember> HaksaMembers{get; set;} //Didn't fix here because table structure is the same
        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
        }

Then I modified the linq part:

using (var haksaContext = new OracleContext())
{
    try{
        var haksa = haksaContext.HaksaMembers.Where(m => m.stnt_numb == model.Stnt_Numb).ToList();
        if (haksa.Count == 1){

But it doesn't retrieve data, and haksa. Count is 0

I tried debug and found out the connection string is strange. In my web.config I used this

<add name="OracleContext" providerName="Oracle.ManagedDataAccess.Client" connectionString="User Id=myid;Password=mypw;(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=targetdbIP)(PORT=1521)))(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=haksadb)))" />

connectionString,数据源

It's far from my connectionString in web.config.. where is this connectionString came from..

Data Source=(localdb)\mssqllocaldb;AttachDbFilename=|DataDirectory|OraleContext.mdf;Initial Catalog=OraleContext;Integrated Security=True;MultipleActiveResultSets=True

If I debug from original project that part shows exactly the same connectionString as I typed in web.config

By default Entity Framework uses the connection string with the same name as your DbContext.

In your case, change the connection string to

<add name="HaksaContext" providerName="Oracle.ManagedDataAccess.Client" connectionString="..." />

I know it's probably too late :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM