-
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
Can't convert byte[] to string #20159
Comments
@sbsw What would you expect to happen with the existing binary data? |
As the warning says, “you’ve scaffolded a change that might result in loss of data”. I understand that I’ve made a breaking change and I expect the data to be deleted. I worked around the problem by modifying the generated code to first delete the Now that I think about it, I don't understand why I don't see the same problem with the In addition to the existing (highlighted) warning that I get when I run |
@sbsw We discussed this, but it's not something we think we can/should change at the EF level. (We could implement column re-building, and then replicate the (quite strange) conversion rules that SQL Server has to do a rebuild when necessary, but the value seems low since we still wouldn't be able to convert the data automatically.) @bricelam I think we discussed in the past adding a comment to the generated migration for potential data loss. Do you remember the conclusion? |
@ajcvickers I don't know what "this" is that you discussed. Just to be clear, I'm not asking for the migration code to do anything fancy. I understand that I've introduced a breaking change, and I'm fine with it deleting the existing data in the affected column. My issue is simply that the scaffolded migration code fails with an exception, so I can't execute the scaffolded migration. The scaffolded migration code should execute without error. If the code throws away data (or may throw away out-of-range or otherwise incompatible data) then it would be nice if we were told about it, in a warning from the scaffolder (as it does today), in a comment in the code, and in a warning from the update-database command that executes the scaffolded code. |
@sbsw "This" refers to "The scaffolded migration code should execute without error." |
@ajcvickers Ouch! So, by design, the scaffolded migration code may look correct (in this case, it appears to be converting a column from Ok, so, instead of giving us the generic "migration may cause loss of data" warning (which usually means I've deleted a column or some such), could you give us a stern, clear warning when the migration code might fail in a more unexpected way? Something like:
|
@sbsw It is always the case that you should test migrations before using them. Ideally, we would report any issue that any database provider might have with a given migration, but that's not practical. That's why we have a tool to scaffold a starting point for a migration, which the developer must review, test, and edit appropriately before using. (This is also why they aren't marked as "auto-generated code"--they are intended as a starting point only. Unfortunately, this often isn't how people use migrations. We have discussed this mismatch in understanding before, but I think it's worth us discussion again. I'll bring it up with the team. |
@ajcvickers Hehe... Yeah, we get to the point were we rely on your tools to do what we "expect" them to do. That perception is reinforced by the generally excellent warnings that we get when the tool suspects that something may require our attention. Maybe you want to just be more explicit in setting expectations via a comment block at the beginning of the scaffolded code:
|
If we had more information in the provider about implicit conversions between store types (also needed for #15586), we could handle this better. |
Comment made by @Brar (who is sitting next to me): we could also scaffold a commented-out proposal of a migration, with the conversion encoding as a placeholder. This would go a long way to help the user deal with this, while forcing them to actually think about it (and put in a valid encoding). This makes sense especially since we're saying we expect users to inspect our migrations etc. |
The problem (from my perspective as a developer who relies on the tooling to just get the job done) is that the migration code is a high-level abstraction (add this column; change that column from The only realistic option is for the user to test both the Up and Down migration every time to see if it throws an exception. If it passes that simple test then I assume that the database schema was correctly modified. It would be really helpful if the migration methods were smart enough to guess what operations MIGHT cause corruption or data loss, and emit a warning during the |
@sbsw note that there are many ways in which you can add a migration which would fail; for example, you could set a default SQL value that isn't valid, EF Core would create that migration and SQL Server would error when you tried to apply the migration. It's simply not possible for EF Core to attempt to identify all cases in which a migration would cause an error upon application. As @ajcvickers wrote above, in any real-world production scenario, migrations must be inspected and applied against a test/preproduction database, surfacing this kind of issue. |
Notes from team triage:
|
I changed the Type of a property from
byte[]
tostring
. The resultant migration code in theDown
routine is:When I attempt to
update-database
to an earlier migration, which runs the above code, I get this error:The full error message is:
Steps to reproduce
Further technical details
EF Core version:
Database provider: (e.g. Microsoft.EntityFrameworkCore.SqlServer)
Target framework: (e.g. .NET Core 3.0)
Operating system:
IDE: (e.g. Visual Studio 2019 16.3)
The text was updated successfully, but these errors were encountered: