microsoft / DacFx

DacFx, SqlPackage, and other SQL development libraries enable declarative database development and database portability across SQL versions and environments. Share feedback here on dacpacs, bacpacs, and SQL projects.
https://aka.ms/sqlpackage-ref
MIT License
308 stars 19 forks source link

How get the default value if the specific field #400

Open LmG7119 opened 3 years ago

LmG7119 commented 3 years ago

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 + ",");
                    }
                }
ErikEJ commented 3 years ago

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

LmG7119 commented 3 years ago

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

ErikEJ commented 3 years ago

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