Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
238 views
in Technique[技术] by (71.8m points)

c# - Set database options in EF Core 3.1

I'm using EF Core 3.1 with Model-First migrations. If the database doesn't exist, then it automatically creates one.

I need to enable SQL Server change tracking (NOT EF Core change tracking) on a table, but to do so, SQL Server Database Change tracking needs to be turned on. How would I set a database level option such as below in code?

SET CHANGE_TRACKING = ON  
(CHANGE_RETENTION = 2 DAYS, AUTO_CLEANUP = ON)  
question from:https://stackoverflow.com/questions/65930111/set-database-options-in-ef-core-3-1

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
  • your can use raw sql
    using(var context = new SampleContext())
    {
        var commandText = "INSERT Categories (CategoryName) VALUES (@CategoryName)";
        var name = new SqlParameter("@CategoryName", "Test");
        context.Database.ExecuteSqlCommand(commandText, name);
    }
  • or execute sql when migration
public partial class UpdateDbOptions: Migration
{
    protected override void Up(MigrationBuilder migrationBuilder)
    {
        var sp = @"ALTER DATABASE [mydatabase]  
SET CHANGE_TRACKING = ON  
(CHANGE_RETENTION = 2 DAYS, AUTO_CLEANUP = ON)";

        migrationBuilder.Sql(sp);
    }

    protected override void Down(MigrationBuilder migrationBuilder)
    {

    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...