简体   繁体   中英

Using DbSet.Find with Oracle and EF CodeFirst

I've read in a lot of websites and particulary in oracle forums that Entity Framework Code First or DbContext API is NOT officially supported by Oracle.

That being said, I've also read in a lot of places that people have succesfully deployed projects using EF CodeFirst with Oracle. I've been running through a lot of issues and have been adjusting the code in order to make it work with SQL Server too.

One function that does not seem to work is DbSet.Find where you pass the key as parameter and gets exactly that record.

Using Find (which works just fine if I use sql server on the same codebase) throws an exception with the following error:

ORA-00942: table or view does not exist

I read this question about it and they mention casing, which is not my case so no fix for me.

The odd thing is that using DbSet.SingleOrDefault(x => x.Id == SomeId) works just fine so I'm pretty sure the casing of the schema, table names and columns is fine.

Has anyone successfully used Find when using DbContext?

Please do note: I'm not using an EDMX and can't use it, we must continue using CodeFirst.

Thanks.

Well it turns out that there was indeed a problem with the data annotations made in my classes and I had a typo on one of them but the exception does not expose the referenced table or view so the only way to find out was to check that:

[TableName="MY_TABLE"]
public class MyClass
{

}

Corresponded to the table names

CREATE TABLE MY_TABLE {

}

...for all of my classes that had a DbSet on my DbContext implementation.

Now, the reason why DbSet.Find() was throwing the exception and DbSet.SingleOrDefault() was not is beyond my knowledge but I assume that the first traverses all of your POCO classes and tries to map them in the DbContext object even if the DbSet you are working on does not relate to the others on this same DbContext, on the other hand, SingleOrDefault() does seem to check mappings for only those tables and objects you are actually going to use when querying.

So yes, I assumed that since DbSet.SingleOrDefault() was not throwing and exception but DbSet.Find() was, the problem was on EF and not on my mappings.

Different extension methods have different behaviors so be careful and pay attention to those mappings!

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