R4ND3LL / EntityFrameworkRuler

Automate the customization of the Entity Framework Reverse Engineered model, including EDMX support for EF6 upgrades.
MIT License
20 stars 5 forks source link

Self-referencing Navigations Not Generated #2

Closed 2xRon closed 1 year ago

2xRon commented 1 year ago

First, let me say, workflow-wise this is shaping up to be exactly what we're looking for and will probably replace EFPT for us.

We have a number of tables that use self-referencing columns as a sort of history table. It seems that scaffolding with EFR skips the navigation and reverse navigation properties for those FKs (the correct column name is still generated).

R4ND3LL commented 1 year ago

Thanks, @2xRon. It does seem to be shaping up, filling a few gaps in the reverse engineer process.

Seems odd since the self referencing nav is nothing special.

Can you confirm that scaffolding without EFR Design referenced does generate the navs normally?

Also, does the json file have the correct FK names for those relations? The names are gathered from the EDMX but sometimes that goes out of sync from the DB if changed afterwards in the DB.

For one of the tables in question, set "IncludeUnknownColumns" to true and see if that resolves it.

2xRon commented 1 year ago

Ah that first point was the main problem. Looks like these relationships/navigations only exist in the edmx and don't have corresponding FK constraints in the DB for those columns. I get it if that's outside the scope of this project.

R4ND3LL commented 1 year ago

Currently, elements that are customized have to at least exist in the DB. However it's conceivable to build extra navigations on the fly.

This is something I'd have to plan for a bit later if there's call for it.

R4ND3LL commented 1 year ago

@2xRon, as a simple workaround, turn on config file splitting in the rules.json:

"SplitEntityTypeConfigurations": true

This will give you separate entity config files within partial classes so that you can easily extend the configuration using:

partial void OnConfigurePartial(..) {}

Create your additions in a separate file and they wont be overwritten each time you scaffold.

2xRon commented 1 year ago

Yup, this is what I'm doing now - nifty feature partial classes/functions is!

As an aside, is it intended that the dbset names don't get pluralized if the entity is renamed? I can open that as a new ticket.

R4ND3LL commented 1 year ago

As an aside, is it intended that the dbset names don't get pluralized if the entity is renamed? I can open that as a new ticket.

Open away - that shouldn't be.

R4ND3LL commented 1 year ago

DbSet pluralization fixed in v1.1.6 Also added json property DbSetName to navigations for customization. VS Extension v1.0.69

R4ND3LL commented 1 year ago

FYI, design time navigations are now supported, such as adding navigations to views. Table splitting and inheritance is also supported since the creation of this issue. You would need to regenerate a json file from the EDMX, however, as there is additional FK mapping information that EFR has to capture. Then scaffold as usual.

2xRon commented 1 year ago

That's awesome, you're the best!

I gave it a try, but it seems like ~80% of the navigations aren't generated with the message "...does not form a valid constraint." Many (but not all) of them are self-referencing navigations (parent row etc). I'd like to help, but I'm not quite sure how to attach the debugger at design-time.

R4ND3LL commented 1 year ago

that error would be raised when the constraint's dependent columns or principal columns are not found on the table. The column count on each side of the constraint should also match (normally 1).

Can you check one of the FKs in question and verify that the definition of that FK in the JSON does have columns defined and that they exist on the table? In the Rule Editor, you'll see them on the FOREIGNKEYS tab (when root node selected)

2xRon commented 1 year ago

Checked out a few broken ones, they look Ok to me. One thing different is the PK column on the principle end tends to have a Name, PropertyName, and a NewName defined with only a casing difference between Name and PropertyName.

R4ND3LL commented 1 year ago

If I can reproduce the issue then I can resolve it. Would it be possible to provide an EDMX+DB creation script (or at least a reduced down version that exhibits the problem)? If you want, I can pass my email over so that you can send it directly.

2xRon commented 1 year ago

Sure thing. SampleDatabaseEdmx.zip

R4ND3LL commented 1 year ago

k. in that case, you can attach a minimal sample directly to your github comment.

R4ND3LL commented 1 year ago

Reproduced using your sample and fixed in v1.2.17. Thanks!

You'll want to regenerate your rules to be safe. Scaffolding should work fine then.

2xRon commented 1 year ago

Awesome work! I found two small wrinkles:

Sample: SampleDatabaseEdmx.zip

1 is on the BuildingAddress table, and 2 is on the PersonAddress-Address navigation.

R4ND3LL commented 1 year ago

Before I get into the scenario, I can tell you the second issue may be due to the IncludeUnknownColumns flag being false on the entity rule. so it's excluding the navigation because it doesn't recognize the FK.
I'll get back to you soon

R4ND3LL commented 1 year ago

That's a first-class sample you put together. Makes my job easy. thanks.

1 is fixed on build 1.2.19. 2 is not reproducible. Nav PersonAddress-Address appears when I scaffold your sample. Unless I'm missing something, weren't you saying the nav was missing?

2xRon commented 1 year ago

Fix confirmed, thank you! For two, it looks like it working when I don't pass a "--no-build" flag to the scaffolder.

R4ND3LL commented 1 year ago

Can you confirm what you're seeing when you do pass "--no-build"? PersonAddress.Address nav is missing?

2xRon commented 1 year ago

I'm not really sure what happened, I tried again and it's working fine now. Thanks for the help!

R4ND3LL commented 1 year ago

When you run the scaffolding, it generates/updates the rule json file. It sounds like may be an issue that existed on a previous run, but after the json file updated, the issue would no longer occur. i.e. some metadata could have been misreported in the edmx. i'll generate a new json file from the edmx and see if it happens