简体   繁体   English

将DbSet.Find与Oracle和EF CodeFirst一起使用

[英]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. 我在甲骨文论坛上的很多网站上特别是在Oracle官方不支持Entity Framework Code First或DbContext API的情况下均已读到。

That being said, I've also read in a lot of places that people have succesfully deployed projects using EF CodeFirst with Oracle. 话虽这么说,但我也读过很多地方,人们已经使用带有Oracle的EF CodeFirst成功部署了项目。 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. 我一直在处理很多问题,并且一直在调整代码以使其也可以与SQL Server一起使用。

One function that does not seem to work is DbSet.Find where you pass the key as parameter and gets exactly that record. 似乎不起作用的一个函数是DbSet.Find,您可以在其中将键作为参数传递并准确地获取该记录。

Using Find (which works just fine if I use sql server on the same codebase) throws an exception with the following error: 使用查找(如果我在同一代码库上使用sql server,效果很好)抛出异常,并显示以下错误:

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. 奇怪的是,使用DbSet.SingleOrDefault(x => x.Id == SomeId)可以很好地工作,所以我很确定模式,表名和列的大小写都可以。

Has anyone successfully used Find when using DbContext? 使用DbContext时,有人成功使用过Find吗?

Please do note: I'm not using an EDMX and can't use it, we must continue using CodeFirst. 请注意:我没有使用EDMX,也无法使用它,我们必须继续使用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. ...对于我在DbContext实现上具有DbSet的所有类。

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. 现在, DbSet.Find()异常而DbSet.SingleOrDefault()原因并非我所知,但我认为第一个遍历所有POCO类并尝试将它们映射到DbContext对象中,即使您正在处理的DbSet与同一DbContext上的其他对象不相关,另一方面, SingleOrDefault()似乎仅检查查询时实际上要使用的那些表和对象的映射。

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. 所以是的,我假设由于DbSet.SingleOrDefault()没有DbSet.Find()异常,而DbSet.Find()DbSet.Find()异常,所以问题出在EF而不是我的映射上。

Different extension methods have different behaviors so be careful and pay attention to those mappings! 不同的扩展方法具有不同的行为,因此请小心并注意那些映射!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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