C
C#ā€¢16mo ago
Zess

āœ… Change Table Row dataType?

How can I change it to Int to Varchar?
71 Replies
friedice
friediceā€¢16mo ago
alter table {table name} alter column {columnname} varchar not null
Zess
ZessOPā€¢16mo ago
where I'll write this?
Zess
ZessOPā€¢16mo ago
friedice
friediceā€¢16mo ago
as a query to your db i never used VS that way, I just use mssms
Kouhai
Kouhaiā€¢16mo 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ā€¢16mo ago
yes wait no I made the class first
Kouhai
Kouhaiā€¢16mo ago
You're doing code first? Okay so code first
Zess
ZessOPā€¢16mo ago
yes
Kouhai
Kouhaiā€¢16mo ago
Then don't modify the database manually Change the class and apply the new migration
Zess
ZessOPā€¢16mo ago
I changed it here
Kouhai
Kouhaiā€¢16mo ago
No no no
Zess
ZessOPā€¢16mo ago
but in database it doesn't change
Kouhai
Kouhaiā€¢16mo ago
Don't change the generated migration class manually
Zess
ZessOPā€¢16mo ago
uff šŸ™‚
Kouhai
Kouhaiā€¢16mo ago
Can you show the class iteself?
Zess
ZessOPā€¢16mo ago
yes
Kouhai
Kouhaiā€¢16mo ago
The class for the joke table
Zess
ZessOPā€¢16mo ago
Zess
ZessOPā€¢16mo ago
i change it later make Joke ans int to string
Kouhai
Kouhaiā€¢16mo ago
Alright, apply a new migration then!
Zess
ZessOPā€¢16mo ago
can you tell me how? Add-Migration "setup2" like this?
Kouhai
Kouhaiā€¢16mo 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ā€¢16mo ago
how can I delete the previous migration?
Zess
ZessOPā€¢16mo ago
when I use update
Kouhai
Kouhaiā€¢16mo ago
Hmm, can you revert the changes first?
Zess
ZessOPā€¢16mo ago
idk how šŸ™‚ im new
Kouhai
Kouhaiā€¢16mo ago
Okay, do you have any data in the Joke table?
Zess
ZessOPā€¢16mo ago
should I delet this?
Zess
ZessOPā€¢16mo ago
no
Kouhai
Kouhaiā€¢16mo ago
Alright, open visual studio's git tools
Zess
ZessOPā€¢16mo ago
Zess
ZessOPā€¢16mo ago
I should start from the first my bad
Kouhai
Kouhaiā€¢16mo ago
Kouhai
Kouhaiā€¢16mo ago
Sorry it's called Git changes šŸ˜…
Zess
ZessOPā€¢16mo ago
Kouhai
Kouhaiā€¢16mo ago
Right click on the InitialSetup.cs file here and "undo changes"
Zess
ZessOPā€¢16mo ago
done
Zess
ZessOPā€¢16mo ago
now? update/ ?
Kouhai
Kouhaiā€¢16mo ago
Okay remove the last migration Remove-Migration
Zess
ZessOPā€¢16mo ago
it said done
Kouhai
Kouhaiā€¢16mo ago
What do you have now in Data\Migrations folder?
Zess
ZessOPā€¢16mo ago
Kouhai
Kouhaiā€¢16mo ago
Alright now Add a new migration
Zess
ZessOPā€¢16mo ago
should I remove & start a new migration?
Kouhai
Kouhaiā€¢16mo ago
Add-Migration "name_goes_here"
Zess
ZessOPā€¢16mo ago
what about initial setup?
Kouhai
Kouhaiā€¢16mo ago
Keep it like that
Zess
ZessOPā€¢16mo ago
Kouhai
Kouhaiā€¢16mo ago
Now update your database Update-Database
Zess
ZessOPā€¢16mo ago
Kouhai
Kouhaiā€¢16mo ago
View the joke table now
Zess
ZessOPā€¢16mo 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ā€¢16mo 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ā€¢16mo ago
but what about initialsetup?
Zess
ZessOPā€¢16mo ago
how to drop it?
Kouhai
Kouhaiā€¢16mo 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ā€¢16mo ago
yes understood but what if i want to delete it?
Kouhai
Kouhaiā€¢16mo ago
Delete it from the source code or the database as well? Because the database keeps track of migrations applied
Zess
ZessOPā€¢16mo ago
I understant
Zess
ZessOPā€¢16mo ago
it just override the method if I delete it the db won't work right?
Kouhai
Kouhaiā€¢16mo 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ā€¢16mo ago
oh so after finishing the project I can delete if i want right?
Kouhai
Kouhaiā€¢16mo ago
You can, but you shouldn't really delete it, you should keep your migrations, but if you want to sure
Zess
ZessOPā€¢16mo ago
got it. can I dm you? I need few help but offtopic.
Kouhai
Kouhaiā€¢16mo ago
I prefer to keep communication here šŸ˜„
Zess
ZessOPā€¢16mo 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ā€¢16mo 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ā€¢16mo ago
oh
Kouhai
Kouhaiā€¢16mo 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ā€¢16mo ago
thanks for your help ā¤ļø
Kouhai
Kouhaiā€¢16mo ago
Np Make sure to /close
Want results from more Discord servers?
Add your server