DacFx icon indicating copy to clipboard operation
DacFx copied to clipboard

How get the default value if the specific field

Open LmG7119 opened this issue 4 years ago • 3 comments

I'm using t4 C3 script to create some SQL server object dynamically from predefined tables. Everything works except default values Example

intake.TabA(
id int not null,
Name varchar(10) not null default('')
)

I need to generate persist table as follows

persist.TabA(
id int not null,
Name varchar(10) not null **default('')**
DateEff datetime,
DateExp datetime
)

I can generate everything but not default('') Here is the snippet

             foreach (var col in table.GetReferenced(Table.Columns))
                {

                    string columnText;
                    string columnName = col.Name.Parts[2];
                    //string columnName3 = col.Name.Parts[3];
                    //string columnName4 = col.Name.Parts[4];


                    // this attempts to limit to only columns from the source. there's gotta be a cleaner way.
                    if (!skipColumns.Contains(columnName))
                    {

                        int length = col.GetProperty<int>(Column.Length);
                        int precision = col.GetProperty<int>(Column.Precision);
                        int scale = col.GetProperty<int>(Column.Scale);
						//string defaultExpression = col.GetProperty<string>(Column.Expression).ToString();

                        string suffix;
                        if (length != 0)
                        {
                            suffix = String.Format("({0})", length);
                        }
                        else if (precision != 0)
                        {
                            suffix = String.Format("({0},{1})", precision, scale);
                        }
                        else if (precision == 0 && scale != 0)
                        {
                            suffix = String.Format("({0})", scale);
                        }
                        else
                        {
                            suffix = "";
                        }

                        bool nullable = col.GetProperty<bool>(Column.Nullable);
                        string nullText = nullable ? "NULL" : "NOT NULL";

                        string dataType = col.GetReferenced(Column.DataType).FirstOrDefault().Name.ToString();
						//string defaultExpression = "";
						//defaultExpression = col.GetReferenced(Column.Expression).FirstOrDefault().Name.ToString();
                        ////string dataType = col.GetReferenced(Column.Con)
						

                        columnText = String.Format("[{0}] {1}{2} {3} {4}", columnName, dataType, suffix, nullText, defaultExpression);

						WriteLine("			" + columnText + ",");
                    }
                }

LmG7119 avatar Feb 24 '21 22:02 LmG7119

I can get defaults from the strongly typed lib: https://github.com/ErikEJ/EFCorePowerTools/blob/master/src/GUI/ErikEJ.EntityFrameworkCore.SqlServer.Dacpac/Scaffolding/SqlServerDacpacDatabaseModelFactory.cs

ErikEJ avatar Feb 25 '21 20:02 ErikEJ

I'm not much of the C# developer

I tried it like this defaultExpression = col.GetReferenced(Column.Expression).FirstOrDefault().Name.ToString(); Any other help would be appreciated.

Thanks

LmG7119 avatar Feb 25 '21 22:02 LmG7119

You need to get all the default constraints, and find the one for the current column.

ErikEJ avatar Feb 26 '21 06:02 ErikEJ