C
C#•2y ago
Zess

āœ… Change Table Row dataType?

How can I change it to Int to Varchar?
71 Replies
friedice
friedice•2y ago
alter table {table name} alter column {columnname} varchar not null
Zess
ZessOP•2y ago
where I'll write this?
Zess
ZessOP•2y ago
friedice
friedice•2y ago
as a query to your db i never used VS that way, I just use mssms
Kouhai
Kouhai•2y ago
@ZESS It looks like you're using ef, are you doing db first? If not, then you shouldn't manually modify the database
Zess
ZessOP•2y ago
yes wait no I made the class first
Kouhai
Kouhai•2y ago
You're doing code first? Okay so code first
Zess
ZessOP•2y ago
yes
Kouhai
Kouhai•2y ago
Then don't modify the database manually Change the class and apply the new migration
Zess
ZessOP•2y ago
I changed it here
Kouhai
Kouhai•2y ago
No no no
Zess
ZessOP•2y ago
but in database it doesn't change
Kouhai
Kouhai•2y ago
Don't change the generated migration class manually
Zess
ZessOP•2y ago
uff šŸ™‚
Kouhai
Kouhai•2y ago
Can you show the class iteself?
Zess
ZessOP•2y ago
yes
Kouhai
Kouhai•2y ago
The class for the joke table
Zess
ZessOP•2y ago
Zess
ZessOP•2y ago
i change it later make Joke ans int to string
Kouhai
Kouhai•2y ago
Alright, apply a new migration then!
Zess
ZessOP•2y ago
can you tell me how? Add-Migration "setup2" like this?
Kouhai
Kouhai•2y ago
I guess so, I usually use dotnet ef migrations add "name_goes_here" But I think Add-Migration "name_goes_her" works This will generate a new migration And then Update-Database Also make sure to revert the changes made to InitalSetup class
Zess
ZessOP•2y ago
how can I delete the previous migration?
Zess
ZessOP•2y ago
when I use update
Kouhai
Kouhai•2y ago
Hmm, can you revert the changes first?
Zess
ZessOP•2y ago
idk how šŸ™‚ im new
Kouhai
Kouhai•2y ago
Okay, do you have any data in the Joke table?
Zess
ZessOP•2y ago
should I delet this?
Zess
ZessOP•2y ago
no
Kouhai
Kouhai•2y ago
Alright, open visual studio's git tools
Zess
ZessOP•2y ago
Zess
ZessOP•2y ago
I should start from the first my bad
Kouhai
Kouhai•2y ago
Kouhai
Kouhai•2y ago
Sorry it's called Git changes šŸ˜…
Zess
ZessOP•2y ago
Kouhai
Kouhai•2y ago
Right click on the InitialSetup.cs file here and "undo changes"
Zess
ZessOP•2y ago
done
Zess
ZessOP•2y ago
now? update/ ?
Kouhai
Kouhai•2y ago
Okay remove the last migration Remove-Migration
Zess
ZessOP•2y ago
it said done
Kouhai
Kouhai•2y ago
What do you have now in Data\Migrations folder?
Zess
ZessOP•2y ago
Kouhai
Kouhai•2y ago
Alright now Add a new migration
Zess
ZessOP•2y ago
should I remove & start a new migration?
Kouhai
Kouhai•2y ago
Add-Migration "name_goes_here"
Zess
ZessOP•2y ago
what about initial setup?
Kouhai
Kouhai•2y ago
Keep it like that
Zess
ZessOP•2y ago
Kouhai
Kouhai•2y ago
Now update your database Update-Database
Zess
ZessOP•2y ago
Kouhai
Kouhai•2y ago
View the joke table now
Zess
ZessOP•2y ago
its good now can you tell me what you did? if i face problem like this should I delete the migration & create new? or what?
Kouhai
Kouhai•2y ago
Sure thing First we reverted the changes you manually made to the migrations file (the reason you had to revert it is because when a migrations class is generated it compares itself with the previous one, to keep track of whether the new migrations needs to change the column type or name, when you manually changed it, the generator basically can't really make sense of what's the difference) Second we create a new migration with the updated column type Third apply the new migration to the database
Zess
ZessOP•2y ago
but what about initialsetup?
Zess
ZessOP•2y ago
how to drop it?
Kouhai
Kouhai•2y ago
You don't need to drop it, you only need to apply changes based on what's different between initialsetup and Dbnew
Zess
ZessOP•2y ago
yes understood but what if i want to delete it?
Kouhai
Kouhai•2y ago
Delete it from the source code or the database as well? Because the database keeps track of migrations applied
Zess
ZessOP•2y ago
I understant
Zess
ZessOP•2y ago
it just override the method if I delete it the db won't work right?
Kouhai
Kouhai•2y ago
Well, if you delete it the database will work, because after migrations are applied, they don't affect the database (until you need to apply a new migartion)
Zess
ZessOP•2y ago
oh so after finishing the project I can delete if i want right?
Kouhai
Kouhai•2y ago
You can, but you shouldn't really delete it, you should keep your migrations, but if you want to sure
Zess
ZessOP•2y ago
got it. can I dm you? I need few help but offtopic.
Kouhai
Kouhai•2y ago
I prefer to keep communication here šŸ˜„
Zess
ZessOP•2y ago
or can i say it here? can you give me some discord link related to dotNet? & resources to learn dotnet core mvc?
Kouhai
Kouhai•2y ago
I don't personally know any specific links for asp.net core mvc You can check the official docs or ask over #web
Zess
ZessOP•2y ago
oh
Kouhai
Kouhai•2y ago
If you do want to ask any dotnet questions, you won't really find anything better than here (unless it's unity specific for example)
Zess
ZessOP•2y ago
thanks for your help ā¤ļø
Kouhai
Kouhai•2y ago
Np Make sure to /close

Did you find this page helpful?