Thứ Năm, 5 tháng 9, 2024

How does Entity Framework Core open and close the SqlConnection?

Entity Framework Core manages the SqlConnection lifecycle internally, ensuring that connections are opened and closed as needed. Here's a detailed explanation of how this process works:

Opening the Connection

Entity Framework Core opens the connection in a lazy manner, meaning it opens the connection only when it is needed. This typically happens in the following scenarios:

  • Query Execution: When a query is executed against the database.
  • Save Changes: When changes are saved to the database using context.SaveChanges().
  • Explicit Opening: When you explicitly open the connection using context.Database.OpenConnection().

Closing the Connection

Entity Framework Core closes the connection automatically when it is no longer needed. This typically happens in the following scenarios:

  • After Query Execution: The connection is closed after the query results are retrieved.
  • After Save Changes: The connection is closed after the changes are saved to the database.
  • Disposing the DbContext: The connection is closed when the DbContext is disposed, which usually happens at the end of a using block or when the DbContext is explicitly disposed.
  • Explicit Closing: When you explicitly close the connection using context.Database.CloseConnection().

Example Code

Here is an example demonstrating how Entity Framework Core manages the connection lifecycle:

Best Practices

  • Using using Statement: Always use the using statement to ensure that the DbContext is disposed properly, which in turn ensures that the connection is closed.
  • Avoid Explicit Management: Let Entity Framework Core manage the connection lifecycle unless you have a specific reason to manage it manually.

Internals

Internally, Entity Framework Core uses the DbConnection object to manage the connection. Here is a simplified view of what happens:

  1. Query Execution:

    • Entity Framework Core checks if the connection is open.
    • If the connection is not open, it opens the connection.
    • Executes the query.
    • Closes the connection if it was opened by Entity Framework Core.
  2. Save Changes:

    • Entity Framework Core checks if the connection is open.
    • If the connection is not open, it opens the connection.
    • Saves the changes.
    • Closes the connection if it was opened by Entity Framework Core.

By following these practices and understanding the internals, you can ensure that the SqlConnection is properly managed and closed, preventing potential connection leaks.

Thứ Hai, 26 tháng 8, 2024

[C#] Check String Is Alphanumeric

    public bool IsAlphanumeric(string input)
    {
        foreach (char c in input)
        {
            if (!char.IsLetterOrDigit(c))
            {
                return false;
            }
        }
        return true;
    }
    public bool IsAlphanumeric(string input)
    {
        var regex = new Regex("^[a-zA-Z0-9]*$");
        return regex.IsMatch(input);
    }

Thứ Bảy, 17 tháng 8, 2024

[C#] Path.Combine for URI

public static class UriPath
{
	public static string Combine(params string[] paths)
	{
		return paths.Aggregate((current, path) => $"{current.TrimEnd('/')}/{path.TrimStart('/')}");
	}
}
public class UriPathTests
{
    [Fact]
    public void Combine_OnePath_ReturnTheOriginal()
    {
        var url = UriPath.Combine("https://stackoverflow.com/questions/372865");
        Assert.Equal("https://stackoverflow.com/questions/372865", url);
    }

    [Fact]
    public void Combine_OnePath_ShouldNotRemoveTheSlashAtTheEnd()
    {
        var url = UriPath.Combine("https://stackoverflow.com/questions/372865/");
        Assert.Equal("https://stackoverflow.com/questions/372865/", url);
    }

    [Theory]
    [InlineData("https://stackoverflow.com/questions/372865", "path-combine-for-urls")]
    [InlineData("https://stackoverflow.com/questions/372865", "/path-combine-for-urls")]
    [InlineData("https://stackoverflow.com/questions/372865/", "path-combine-for-urls")]
    [InlineData("https://stackoverflow.com/questions/372865/", "/path-combine-for-urls")]
    public void Combine_TwoPaths_ReturnSameResult(string uri1, string uri2)
    {
        var url = UriPath.Combine(uri1, uri2);
        Assert.Equal("https://stackoverflow.com/questions/372865/path-combine-for-urls", url);
    }

    [Theory]
    [InlineData("https://stackoverflow.com/questions/372865", "path-combine-for-urls/")]
    [InlineData("https://stackoverflow.com/questions/372865", "/path-combine-for-urls/")]
    [InlineData("https://stackoverflow.com/questions/372865/", "path-combine-for-urls/")]
    [InlineData("https://stackoverflow.com/questions/372865/", "/path-combine-for-urls/")]
    public void Combine_TwoPaths_ShouldNotRemoveTheSlashAtTheEnd(string uri1, string uri2)
    {
        var url = UriPath.Combine(uri1, uri2);
        Assert.Equal("https://stackoverflow.com/questions/372865/path-combine-for-urls/", url);
    }

    [Theory]
    [InlineData("https://stackoverflow.com/questions/372865", "path-combine-for-urls", "xxx")]
    [InlineData("https://stackoverflow.com/questions/372865", "path-combine-for-urls", "/xxx")]
    [InlineData("https://stackoverflow.com/questions/372865", "/path-combine-for-urls", "xxx")]
    [InlineData("https://stackoverflow.com/questions/372865", "/path-combine-for-urls", "/xxx")]
    [InlineData("https://stackoverflow.com/questions/372865/", "path-combine-for-urls", "xxx")]
    [InlineData("https://stackoverflow.com/questions/372865/", "path-combine-for-urls", "/xxx")]
    [InlineData("https://stackoverflow.com/questions/372865/", "/path-combine-for-urls", "xxx")]
    [InlineData("https://stackoverflow.com/questions/372865/", "/path-combine-for-urls", "/xxx")]
    public void Combine_ThreePaths_ReturnSameResult(string uri1, string uri2, string uri3)
    {
        var url = UriPath.Combine(uri1, uri2, uri3);
        Assert.Equal("https://stackoverflow.com/questions/372865/path-combine-for-urls/xxx", url);
    }

    [Theory]
    [InlineData("https://stackoverflow.com/questions/372865", "path-combine-for-urls", "xxx/")]
    [InlineData("https://stackoverflow.com/questions/372865", "path-combine-for-urls", "/xxx/")]
    [InlineData("https://stackoverflow.com/questions/372865", "/path-combine-for-urls", "xxx/")]
    [InlineData("https://stackoverflow.com/questions/372865", "/path-combine-for-urls", "/xxx/")]
    [InlineData("https://stackoverflow.com/questions/372865/", "path-combine-for-urls", "xxx/")]
    [InlineData("https://stackoverflow.com/questions/372865/", "path-combine-for-urls", "/xxx/")]
    [InlineData("https://stackoverflow.com/questions/372865/", "/path-combine-for-urls", "xxx/")]
    [InlineData("https://stackoverflow.com/questions/372865/", "/path-combine-for-urls", "/xxx/")]
    public void Combine_ThreePaths_ShouldNotRemoveTheSlashAtTheEnd(string uri1, string uri2, string uri3)
    {
        var url = UriPath.Combine(uri1, uri2, uri3);
        Assert.Equal("https://stackoverflow.com/questions/372865/path-combine-for-urls/xxx/", url);
    }
}

Thứ Ba, 16 tháng 1, 2024

Disable Fips Algorithm Policy ASP.NET

- Solution 1: Disable FipsAlgorithmPolicy in Windows Registry:
  Set Computer\HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Lsa\FipsAlgorithmPolicy\Enabled to 0

- Solution 2: Modify C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Aspnet.config

  <configuration>
    <runtime>
        <enforceFIPSPolicy enabled="false" />
    </runtime> 
</configuration>

Reference: