Will
Will

Reputation: 10431

C# Create table from DataContext Table?

Is there a built in way to create a table if it does not exist from the DataContext Table class? Or is the only way to use raw sql statements?

Upvotes: 2

Views: 2573

Answers (1)

Kevin Holditch
Kevin Holditch

Reputation: 5303

If you are talking about using the built in linq-to-sql datacontext class then you can always refresh it from the database if you create a new table and it should be picked up.

How about defining the table manually like this:

[Table(Name = "mydb.dbo.myTable")]
public class MyTable 
{
    [Column(Name = "Id", IsPrimaryKey = true)]
    public int Id { get; set; }

    [Column(Name = "ActiveDate")]
    public DateTime? ActiveDate { get; set; }

}

then you can access the table like this:

var table =  _seatingDataContext.Context.GetTable<MyTable>();

Then you can query it or add rows or whatever. Hope that helps.

Upvotes: 2

Related Questions