2

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 1

2

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.

Sign up to request clarification or add additional context in comments.

6 Comments

Ah, so the Refresh method will create the tables?
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.
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.
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
Ya I am now learning that. Does SQL Server support embedding?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.