How can I execute multiple SQL commands at one time with Entity Framework?

后端 未结 2 1335
渐次进展
渐次进展 2021-01-26 07:29

I have the following code:

var sql = System.IO.File.ReadAllText(\"data_tables.sql\");
context.Database.ExecuteSqlCommand(sql);

My data_tables.s

相关标签:
2条回答
  • 2021-01-26 07:59

    You can use EntityFramework.Extended to achieve this using the .Future queries.

    // build up queries
    var q1 = db.Users
        .Where(t => t.EmailAddress == "one@test.com")
        .Future();
    
    var q2 = db.Tasks
        .Where(t => t.Summary == "Test")
        .Future();
    
    // this triggers the loading of all the future queries
    var users = q1.ToList();
    
    0 讨论(0)
  • 2021-01-26 08:10

    Entity Framework exists to make it easier for you to get data from your database to your UI and back, as well as query data using LINQ or extension methods. If your list of SQL statements has something to do with this, then it might be advantageous to do some sort of conversion. If however you're just executing a "dry" list of preprepared SQL statements, then I see no problem with your current setup.

    This has a lot to do with what you're trying to do and it's very hard to suggest a strategy without more information.

    0 讨论(0)
提交回复
热议问题