1

After creating an Entity Framework project with database first, I added a column to the Clients table ('zehut') in SSMS. After updating in VS doing "Update Model From Database", the new column shows up in the diagram but it put it last in the list. I want it to be in the same order where I inserted it in SSMS. I tried to change the order in the code map but it didn't help.

Is there any way to change the order of the columns in the DbModel diagram in VS? (attached screenshots)

enter image description here

enter image description here

The reason it matters is because I'm updating the form's text fields from the DataGridView programmatically so the Row-Index doesn't match.

Try
        txtId.Text = DataGridViewClients.Item(1, e.RowIndex).Value
        txtZehut.Text = DataGridViewClients.Item(2, e.RowIndex).Value
        cmboStatus.Text = DataGridViewClients.Item(3, e.RowIndex).Value

So yes, I can change the order of the form's fields in the code to match the order of the VS db diagram but since the diagram does NOT match the SSMS order, if the diagram changes (if it gets deleted and re-added), the code will be messed up. I'd rather EVERYTHING be in same order from the start to avoid future possible issues.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Zvi Twersky
  • 399
  • 1
  • 5
  • 25
  • Have you tried [updating the model from the database](https://stackoverflow.com/a/2959035/832052)? – djv Aug 05 '17 at 20:35
  • 2
    why does this matter? The column order is generally unimportant for anything, you can display the columns in any order you wish using SQL or .NET code. – ADyson Aug 05 '17 at 21:10
  • As suggested, the order of the properties in the type definition isn't really relevant because they can be displayed in the UI in any order you like. That said, if you really want the EF property order to match the database column order, delete the entity from the EF model and then updated from the database again. The entity will be recreated from scratch and it will thus match the database. Any time you add database columns and update an existing entity, new properties will always be added at the end. – jmcilhinney Aug 06 '17 at 03:01
  • djv, I wrote in my post that I did that. – Zvi Twersky Aug 06 '17 at 03:27
  • Yes, I know if I delete the table and bring it in again it would do this but once I have lots of data and tables will be dependent on each other, deleting tables and bringing them back in would mess things up. I updated my post to explain the reason I need this. Yes, workarounds would be to change the Row index number in the code to but this would be a nightmare trying to figure out what is what later on (unless I add the column name in a comment on each row in the code). The only thing I can think of is instead of using the RowIndex, maybe instead, to look for a match to the column name. – Zvi Twersky Aug 06 '17 at 03:28
  • Here's another reason... the DataGridView is showing the order of the diagram, which is not the correct order I need. – Zvi Twersky Aug 07 '17 at 15:55
  • "workarounds would be to change the Row index number". Why does _row_ Index matter when we're talking about _columns_? The rowIndex refers to which _row_ you want, not which column. However, you can refer to the column by its name rather than its number, which again will make the order in the table and/or EF model irrelevant. See https://msdn.microsoft.com/en-us/library/ms158657(v=vs.110).aspx – ADyson Aug 07 '17 at 21:35
  • ADyson, yes, obviously I'm talking about columns. I was referring to having to re-arrange the lines of code that have the numbers 1, 2, 3, next to the e.RowIndex but of course, the numbers are the columns. Referring by name would work but again, I would have to make a line of code for each of the hundreds of columns of all tables. Shame. – Zvi Twersky Aug 10 '17 at 16:59

0 Answers0