question

JoseArturoElizondo-6659 avatar image
0 Votes"
JoseArturoElizondo-6659 asked ZhiLv-MSFT commented

How can add extra columns with models

Hello, I try add extra columns one context.

Example I have 3 tables tblpro, tblcolumns and tblinfocolumns

I migration the tables. Now I want add columns the tblpro with tblcolumns.

tblpro:
ID | Name | Desc | Price

tblcolumns:
ID | ColumnName | IdColumn

So, I want show a tableTemp where display tblpro with tblcolumns:
ID | Name | ColumnName1 | ColumnName2 | ColumnName3 | ColumnName4 | ... | Desc | Price

Later display the info tblpro with tblinfocolumns

tblinfocolumns:
ID | IdtblPro | IdtblColumns | qty

Some an idea how can do add columns ?

sql-server-generaldotnet-aspnet-generaldotnet-aspnet-core-mvc
· 4
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

If you are using code first then add the new properties to the model(s), create a migration, and issue the database update command. The entity framework code first tutorial covers migrations.


0 Votes 0 ·


Hello AgaveJoe.

I create 3 files on folder Models

tblpro.cs
tblcolumns.cs
tblinfocolumns.cs

add-migration and update-database

0 Votes 0 ·
AgaveJoe avatar image AgaveJoe JoseArturoElizondo-6659 ·

I'm not sure if you are still having problems...

But, adding files to a model folder does NOT affect Entity Framework. The model must be registered in the DbContext which is a fundamental concept. Again, I'm not sure what you're doing but if you are having problems updating the database schema using Code First, then you should read the documentation. The documentation explains each step of the Code First process. Start with the Overview. Then read each of the subsections like creating a model etc.


0 Votes 0 ·

Hi @JoseArturoElizondo-6659,

Now I want add columns the tblpro with tblcolumns.
I want show a tableTemp where display tblpro with tblcolumns:
ID | Name | ColumnName1 | ColumnName2 | ColumnName3 | ColumnName4 | ... | Desc | Price

Generally, after generating the table using EF core migration, if you want to add extra columns to the table, you can add the properties in the Entity Model, then using the add-migration and update-database commands to add the extra columns to the table. Reference: Getting Started with EF Core and EF core

But, from your description, it seems that you want to convert the tblcolumns's row to the column, then join with the tblpro table and filter all columns, right?

Please explain more detail about your question, if possible, I suggest you could create a simple sample with some test data to reproduce the problem, it is easier for us to understand what you want.

0 Votes 0 ·

0 Answers