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?
1 Answer
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.
6 Comments
Will
Ah, so the Refresh method will create the tables?
Kevin Holditch
Well it's probably easier to create the table in the database and then regenerate the database context, after which time the datacontext will contain your new table.
Will
Is it possible to create the context from a database layout? I am trying with sqlce 3.5 and I keep getting not supported errors. I really don't want to have to maintain create tables queries alone with contexts.
Kevin Holditch
Sorry you didnt state you were using SQL Compact Edition in your original question, I'm not sure of the compatibility between that and linq-to-sql contexts
Will
Ya I am now learning that. Does SQL Server support embedding?
|