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

FK without navigation properties #2517

Closed
ilmax opened this issue Jul 1, 2015 · 7 comments
Closed

FK without navigation properties #2517

ilmax opened this issue Jul 1, 2015 · 7 comments

Comments

@ilmax
Copy link
Contributor

ilmax commented Jul 1, 2015

Hi, I searched around the repo, but I didn't found nothing related, so if this is a dupe, feel free to close.

What I would like to have is the ability to define FK without navigation property, even better I would like to create a shadow state FK, to keep my class clean.

I would like to have this feature to implement e.g. a very simple auditing mechanism on each entity, to keep track of the last user that has updated a certain entity, but i would like to keep these fields off my entity.

Now I'm not able to create a FK without a navigation property, so IMHO this could be a quite useful feature.

Regards, Max

@bricelam
Copy link
Contributor

bricelam commented Jul 1, 2015

It's ugly, but possible:

modelBuilder.Entity<Post>(
    x =>
    {
        x.Property<int>("BlogId");
        x.References<Blog>().InverseCollection().ForeignKey("BlogId");
    });

@ilmax
Copy link
Contributor Author

ilmax commented Jul 1, 2015

Thank you @bricelam, it's quite hard to figure it out :) but this is what I need!

@rowanmiller
Copy link
Contributor

Discussed and decided that while the API isn't super awesome for this scenario, it is probably less common and we are ok with sticking with what we have for the moment. We could always add some sugar APIs later if we wanted to.

@ahmad-moussawi
Copy link

Is this still valid as for the version 1.1.1 ?,
I can't find the method Reference of EntityTypeBuilder<T>

@bricelam
Copy link
Contributor

RTM syntax:

modelBuilder.Entity<Post>(
    x =>
    {
        x.Property<int>("BlogId");
        x.HasOne<Blog>().WithMany().HasForeignKey("BlogId");
    });

@ahmad-moussawi
Copy link

@bricelam wonderful :)

@baphamsts
Copy link

could i ask what is benefit of removing Navigation Property from Entity class? Can we use eager loading form then?

@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
None yet
Projects
None yet
Development

No branches or pull requests

6 participants