Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Find Method for a shared entity type #23594

Closed
GLuca74 opened this issue Dec 6, 2020 · 6 comments
Closed

Find Method for a shared entity type #23594

GLuca74 opened this issue Dec 6, 2020 · 6 comments

Comments

@GLuca74
Copy link

GLuca74 commented Dec 6, 2020

Hello All,

I need to use the Find Method for a autogenerate entity type used for a many to many relation, but seems there is no find metod that accepts the entity type name, so if I try :

`
var o= DB.Find(typeof(Dictionary<string, object>), ID, ID2);

`

I get:

Cannot create a DbSet for 'Dictionary<string, object>' because it is configured as an shared type entity type. Access the entity type via theSet` method overload that accepts an entity type name.

`
Is there a way to use the find method for a shared entity type(in this case Dictionary<string, object>)?

Thanks

@ajcvickers
Copy link
Member

@GLuca74 All operations like this must be called on the DbSet for the shared entity type. For example:

context.Set<Dictionary<string, object>>("MyEntityType").Find(ID, ID2);

This is what the exception message you posted says--I'm curious, what was unclear about the exception message? Maybe we can improve it.

@GLuca74
Copy link
Author

GLuca74 commented Dec 7, 2020

Hello @ajcvickers

i am apologide, maybe i explained my point very bad. I try again.

The DBContext class has the Find method that has two overloads, the generic one and the other that accepts the type of the entity as parameter. Before the version 5, the Find Method of the DBContext Class covered all the options, but with the version five it is not possible use it with the shared entity types.

this:
_DBContext.Find(entityType,keyValues)

is usable for any entity type except Shared Entity Types because, with version 5, it was not added an overload that accepts also the entityTypeName

for example something like this :
_DBContext.Find(entityType,"EntityTypeName",keyValues)

The Find methos of DBSet has not the same features because to access it i have to pass by the generic DBSet class.
In my project, I have the entity Type (also the entityTypeName) and the key Values, using DBContext.Find was perfect for me because it accepts the entity type as parameter, while through DBSet i have to get the Set MethodInfo from DBContext Class, make it generic, call it, get the type, get the FindMethod and then call it, all using Reflection:


	    MethodInfo setMethod = typeof(DbContext).GetTypeInfo().GetDeclaredMethods("Set").Single(itm => itm.GetParameters().Length==1);
            var DBSET = setMethod.MakeGenericMethod(entityType).Invoke(_DBContext, new object[] { typeName });
            var FIND= DBSET.GetType().GetTypeInfo().GetDeclaredMethod("Find");
            var o = FIND.Invoke(DBSET, new object[] { keyValues });


while by the find Method of DBContext i could use somenthing like this :

_DBContext.Find(entityType,typeName,keyValues)

similar like i did until the version 5.

it is possible add this overload to use it with shared entity Types?

Many thanks

@ajcvickers
Copy link
Member

ajcvickers commented Dec 7, 2020

Note for triage: this might finally be the valid case for making it easy to create a non-generic DbSet. See #21066.

@smitpatel
Copy link
Member

Or rather: Adding an overload of Find which accepts string entityTypeName. 😉

@ajcvickers
Copy link
Member

@smitpatel It's not just Find. Using the set is also the way to do Add/Update/etc. for shared type entity types.

@ajcvickers
Copy link
Member

Closing as duplicate of #21066

@ajcvickers ajcvickers reopened this Oct 16, 2022
@ajcvickers ajcvickers closed this as not planned Won't fix, can't repro, duplicate, stale Oct 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants