-
Notifications
You must be signed in to change notification settings - Fork 3.2k
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
Unbound navigations for migration purposes have regressed #20796
Comments
The scenario you describe was not supported with the issue you linked either, unbound navigations are only supported on entity types which are in shadow state (i.e. without any backing CLR type). |
FYI I suggested (offline) to simply try a relationship without a navigation property, and in addition making the foreign key a shadow property. @NinoFloris let us know if that works for you. |
@smitpatel I wasn't able to let migrations handle shadow types, dotnet ef threw an exception in the direction of "a valid model Bar cannot be a shadow type" |
Unbounded property from migration context only works inside the model created by ModelSnapshot, which is used in diffing. |
@roji thanks that absolutely works well enough, silly of me to miss it, I have even used this api a few years back in another app... @smitpatel can you elaborate? I'm not sure what the difference is. |
EF Core does not support shadow entity types. See #749 tl;dr - As of now, you cannot use a shadow type or a shadow navigation in OnModelCreating. Can you elaborate that what you mean by "for migration purposes"? OnModelCreating is used to build the model for all purposes. There is no model building which user can do which is specific for migration. |
@smitpatel exactly, this is what I thought. This is the crux of it, if the feature does exist, how can I opt into building shadow entities, if I purely want them specified for migration purposes. I don't want or need them to exist at runtime, I just need them to be managed by EF Core's migration tooling. The reason is simple, a legacy codebase we're 'strangling' currently manages migrations, we want to cut this over to EF Core. However we need to retain some of these legacy tables and foreign keys as the legacy app is still actively querying that database. |
Can you elaborate how do you want them to be managed? It looks to me that in your scenario, you are mapping it to an existing database. If you are not using EF Core to create the database, then migration do not touch any additional objects you have in the database. So I expect even without mapping those legacy tables in EF model, they will remain as is in the database after using migrations. |
@NinoFloris if I understand correctly you want to use EF Core to manage some tables which exist in your legacy database, with including the corresponding CLR types in your code model. Aside from @smitpatel's suggestion to use migrations to manage only some of the tables in the existing database, out of curiosity, is there a specific reason you can't just add CLR types for these tables without actually using them (e.g. under some namespace nobody cares about)? |
@smitpatel with our running databases this works. However we do spin up new databases per tenant from time to time, these tables still need to be there in that case too. Legacy is an active participant in the functionality of the product, sadly it cannot be omitted. @roji no reason, it's how I did it at this moment (empty private classes in the DbContext), my question was one out of curiosity as well ;) |
That would allow someone to successfully specify type If so, that would work really well. |
Yep, that would work. |
Unbound navigations for migration purposes have regressed
This scenario apparently used to be supported.
The usecase here is when you want migrations to track your table "as is", including any foreign keys you may not use in the app (yet).
See this 1.0 issue #2140
Steps to reproduce
Today, trying this:
Throws during
dotnet ef migrations add
:A workaround when the foreign key is named like the type is to do:
However this breaks down when there are multiple foreign keys to the same table (with different column names) as EF will just number them sequentially. (
foo_id
,foo_id1
,foo_id2
etc)I might be able to fix that naming afterwards by digging deep into the metadata but it would be very helpful if the original syntax would just work again. Though if you have a snippet that would do the trick that would be very helpful in the meantime.
Further technical details
EF Core version: 3.1.3
Database provider: EF PG
The text was updated successfully, but these errors were encountered: