question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ColumnExists, AddColumn Extensions

See original GitHub issue

I would like to do this:

//Create Database:
if(!db.TableExists<MyEntity>())
{
    db.CreateTable<MyEntity>(); //Right now I have to try catch that...
}
//Updating db:
//These can ether be on ITable<MyEntity> or db itself
if(!Entities.ColumnExists(e => e.NewProperty)) 
{
    Entities.AddColumn(e => e.NewProperty);
}

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:3
  • Comments:13 (7 by maintainers)

github_iconTop GitHub Comments

2reactions
viceroypenguincommented, Aug 3, 2020

@SlowLogicBoy @frankhaugen

You can create your own extension method to replicate CreateTableIfNotExists as such:

public static void CreateTableIfNotExists<T>(this DataConnection connection, string tableName)
{
    connection.CreateTable<T>(
        "tableName",
        statementHeader: "if not exists (select * from sys.tables where name = 'tmpAccount') begin create table " + tableName,
        statementFooter: "end");
}

You can add additional parameters to pass through to CreateTable() as needed, or switch to CreateTableAsync().

NB: above is for SQL Server only. Adjust for your preferred db.

0reactions
sdanylivcommented, Apr 9, 2022

You can create the following extension probably in separate static class:

public static bool ColumnExists<T>(this DataConnection dataConnection, 
	string  tableName,
        string  columnName,
	string? databaseName = default,
	string? schemaName   = default,
	string? serverName   = default)
	where T : class
{
	return dataConnection.DataProvider.ColumnExists(...);
}

You can use as reference DataConnectionExtensions.BulkCopy

Read more comments on GitHub >

github_iconTop Results From Across the Web

Best practices using SUMMARIZE and ADDCOLUMNS
Extension columns are columns that you add to existing tables. You can obtain extension columns by using both ADDCOLUMNS and SUMMARIZE.
Read more >
c# - EF migrations Code First. Add column to database if ...
I have worked on creating a custom migration method, AddColumnIfNotExists. You need a custom MigrationOperation class:
Read more >
How can I add column to an existed sqlite database?
Currently the new version of my program needs to add a column to the database for a new feature. The program is made...
Read more >
Writing Migrations - 0.13
Columns are added using the addColumn() method. We create a unique index for both the username and email columns using the addIndex() method ......
Read more >
Add Column to List Content Type using PowerShell
Select the appropriate column group and then select the column you want to add to the content type, click on the “Add” button,...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found