5

I can use LINQ to create a database with some constituent tables using datacontext.createDatabase(). The problem is, each time I want to add a new table to the database and use this method, I have to delete the database first and then recreate the whole database with the additional table. This obviously blanks all the contents I had previously populated the tables with.

Is there a way to simply add the new table into the database without recreating it each time from scratch?

0

3 Answers 3

3

I see two ways to do that

first

you can use the ExceuteCommand -method of the DataContext to run some T-SQL statements for creating tables.

and second

you can use this hack function if you have mapped table type, as suggested here

public void CreateTable(Type linqTableClass)
{
    using (var tempDc = new DmsDataContext())
    {
        var metaTable = tempDc.Mapping.GetTable(linqTableClass);
        var typeName = "System.Data.Linq.SqlClient.SqlBuilder";
        var type = typeof(DataContext).Assembly.GetType(typeName);
        var bf = BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.InvokeMethod;
        var sql = type.InvokeMember("GetCreateTableCommand", bf, null, null, new[] { metaTable });
        var sqlAsString = sql.ToString();
        tempDc.ExecuteCommand(sqlAsString);
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

+1 Your first option is what we used to do when we were using L2S. We created a bunch of statements out of programmatically created diff info on the model and executed that. Worked OK.
1

The attached code creates a database, dynamicDatabase11, and a table, books.

public class MyDatabse : DataContext
{
    public Table<Bookss> Bookss;
    public MyDatabse(string connection) : base(connection) { }
}

[Table(Name = "Books")]
public class Bookss
{
    [Column(IsPrimaryKey = true)]
    public string Title;
    [Column]
    public string Rating;
}
class Program
{
    static void Main(string[] args)
    {
        CreateDatabase("DynamicDatabase11");
    }
    public static void CreateDatabase(string Dbname )
    {

        MyDatabse db = new MyDatabse("ConString"+Dbname+"");
        db.CreateDatabase();
    }
}

1 Comment

And how does this answer the OPs question?
0

No, there is no such feature available at this point in time.

Not even in EF4 - at least not now...... these bits here (Code-First Database Evolution) didn't make it into the final EF 4.1 release.... :-(

I hear Microsoft's teams are working hard on a solution (EF "Migrations") - but it's still being worked on

3 Comments

You can, however, have a Sample Data class that will populate your database when recreated. I guess it might work for development.
@nosuchnick: that's in EF 4.1 code-first, right? Don't think Linq-to-SQL has anything similar..
Huh, probably. I'm using both Linq and EF for the first time now, with EF4.1.

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.