Contents tagged with Entity Framework

  • Resilient Connections in Entity Framework Core

    When you work with databases in your application, you may face connection issues from time to time which is beyond our control. When this happens normally the application will raise a connection timeout or server not available exception. In Entity Framework core you can overcome this kind of scenario by setting up resilient connections with exponential retries. 

    The code snippet given below will retry to connect up to 10 times in case of a failure with a delay of 30 seconds in-between each try.

    services.AddDbContext(o =>
    {
        o.UseSqlServer(connectionString,
            sqlServerOptionsAction: options =>
            {
                options.EnableRetryOnFailure(maxRetryCount: 10,
                    maxRetryDelay: TimeSpan.FromSeconds(30),
                    errorNumbersToAdd: null);
            });
    });	
    

    Also, when you enable retries in EF Core connections, each operation you perform will become its own retriable operation. So that means whenever we perform a query or a call to the SaveChanges method, it will be retried as a unit during a transient failure scenario. But when you initiate a transaction block in your code using BeginTransaction, then you are defining your own group that needs to treated as a single unit.

    So, in this case, you will need to manually invoke an execution strategy with a delegate method that contains all that need to be executed as a block. So, when a transient failure occurs, the execution strategy will invoke the delegate again as part of the retry operation.

    var strategy = blogContext.Database.CreateExecutionStrategy();
    await strategy.ExecuteAsync(async () =>
    {
    
        using (var transaction = blogContext.Database.BeginTransaction())
        {
            blogContext.PostItems.Update(postItem);
            await blogContext.SaveChangesAsync();
    
    
            if (raisePostChangedEvent)
            await eventLogService.SaveEventAsync(postChangedEvent);
            transaction.Commit();
        }
    

    Reference : https://docs.microsoft.com/en-us/dotnet/standard/modern-web-apps-azure-architecture/work-with-data-in-asp-net-core-apps


  • Model First Development Using EF 5.0 – Updating the Model

    In my earlier post, Getting started with Model First development using EF, I have shown you to create a database from the model class and how to persist the data to the database. In the second part of the series, I am going to show how the changes made to the underlying database can be reflected in the model class.

     

    So let’s try to add a column to our user table.

    ALTER TABLE MT_USER
    ADD Gender Varchar(5)

     


  • Getting Strated with Model First Development using Entity Framework.

    What is Model First ?

     

    In Model First approach you will create a model of your domain entity using the EF designer and then generate database schema based on it. It stores the meta data in a EDMX file and model classes that interact with our business objects are automatically created from this EDMX file.

    Let’s create a small console application for demonstrating this approach and name the project as EFModelFirst.

     

    clip_image002

     

    I am using VS 2012 and with EF 5.0.0 which can be downloaded from nugget using the command in your package manager console as shown below.

     

    clip_image004