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

query type with fluent api #13286

Closed
herme063 opened this issue Sep 11, 2018 · 7 comments
Closed

query type with fluent api #13286

herme063 opened this issue Sep 11, 2018 · 7 comments
Labels
closed-no-further-action The issue is closed and no further action is planned. customer-reported

Comments

@herme063
Copy link

I am trying to write a fluent configuration for a one to many query type.

public class ObjectA
{
    ...
    public int OwnerId { get; set; } 
    public Owner Owner { get; set; }
    ...
}

public class Owner
{
    public int Id { get; set; }
    public string Name { get; set; }
}

I configured the owner as follow modelBuilder.Query<Owner>().ToView("View_SystemUsers");, and ObjectA has something like this

...
modelBuilder.Entity<ObjectA>
    .HasOne(a => a.Owner)
    .WithMany()
    .HasForeignKey(a => a.OwnerId);
...

This will give me the following error:
System.InvalidOperationException: Unable to create a foreign key with the query type 'Owner' as the principal type. Only entity types are allowed as foreign key principal types.

I am unable to find an one-to-many example for query types, most example online are one-to-one. My case is one to many as one Owner can have multiple ObjectA.

@ajcvickers
Copy link
Member

@herme063 Query types are for mapping types that do not have any property or combination of properties that can act as a unique primary key. This is incompatible with many-to-many relationships since it requires both types on the end of the relationship must have a key.

What is your intention in using query types for this?

@herme063
Copy link
Author

Owners table is in a different database but I want to be able to fetch it with ObjectA without needing a separate step.

@ajcvickers
Copy link
Member

@herme063 It's not clear to me how query types helps with that scenario. Can you give some more details?

@herme063
Copy link
Author

The users of my application can create objects that they own. The user data table is in a different database than the objects table. When I fetch the objects using ef core, I need to pull the related owner/user data(currently just Id and Name).

So, I am planning to create a view; then configure an Owner class to point to that view. I would like for Owner to be read only and it should not be part of code first migration.

May be we can't use query type for this, if so how would we configure the above scenario using fluent api?

@ajcvickers
Copy link
Member

@herme063 If the rows returned from the view have keys, then just map them using entity types in the normal way. Excluding them from migrations is tracked by #2725. Read-only entities is tracked by #7586.

@ajcvickers ajcvickers added the closed-no-further-action The issue is closed and no further action is planned. label Sep 11, 2018
@Heresiarch88
Copy link

@ajcvickers "If the rows returned from the view have keys, then just map them using entity types in the normal way."
What does this mean?
You can't have foreign keys point to a view, so I can't create a navigation object on an entity that points to a view, even if I try to map the view using entities "in the normal way" (but by excluding it from the migration and making it read-only).
That's why I tried using query types, even though the view is based on a table with a PK so it could, in theory, be used in a relationship. But then, entities cannot contain navigation properties to query types...

I pretty much have the same case as the OP (except I use a synonym of a table from a different DB, not a view) and can't figure out how to point to it in a navigation property.

@ajcvickers
Copy link
Member

@Heresiarch88 If you have a view where each row has a unique value that can act as a primary key, the map it as a view, and create a relationship to it in the normal way--that is, the related entity will have a property acting as an FK that correlates with the key values from the view.

@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
Labels
closed-no-further-action The issue is closed and no further action is planned. customer-reported
Projects
None yet
Development

No branches or pull requests

3 participants