Skip to main content

Fluent API EntityFramework 6 C#

Brief

We will develop simple Database Call (SQL Server) using EntityFramework using the Fluent API.

We will have below things for the DB Cal.
  1. Student Class
  2. StudentMap Class
  3. PracticeContext Class
  4. DataBaseCall Class
  5. Proram Class
  6. app.config (In web development, we have web.config)

Student Class:

namespace DotnetConsole
{
  public class Student
  {
    public int ID { get; set; }
    public string Name { get; set; }
  }
}

StudentMap Class

This class is required since fluent api is creating table from the configuration.

public class StudentMap: EntityTypeConfiguration<Student>
  {
    public StudentMap()
    {
      this.ToTable("Student");
      this.Property(s => s.ID).HasDatabaseGeneratedOption(System.ComponentModel.DataAnnotations.Schema.DatabaseGeneratedOption.Identity);
      this.Property(s => s.Name);
    }
  }

PracticeContext Class:

  public class PracticeContext: DbContext
  {
    public PracticeContext(): base() {}
    public DbSet<Student> Students { get; set; }
    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
      base.OnModelCreating(modelBuilder);
      foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies())
      {
        modelBuilder.Configurations.AddFromAssembly(assembly);
      }
    }
  }

DataBaseCall Class:

  public class DataBaseCall
  {
    public static List<Student> GetStudents()
    {
      return Execute((context) =>
      {
        var students = context.Students.ToList();
        if (students != null && students.Count() > 0)
        {
          Console.WriteLine($"Student ID : Student Name");
          foreach (Student student in students)
          {
            Console.WriteLine($"{student.ID} : {student.Name}");
          }
        }
        return students;
      });
    }
    private static TResult Execute<TResult>(Func<PracticeContext, TResult> func)
    {
      try
      {
        return func(new PracticeContext());
      }
      catch (SqlException ex)
      {
        Console.WriteLine(ex);
        throw new Exception("Error Occured.");
      }
    }
}

Program Class

  class Program
  {
    static void Main(string[] args)
    {
      try
      {
        DataBaseCall.GetStudents();
      }
      catch(Exception ex)
      {
        Console.WriteLine("Error occured while performing student operation.", ex);
      }
      Console.ReadKey();
    }
}

App.config file:

Highlighted things are mandatory:


  • entityFramework: Install it from the Nuget Package Manager
  • PracticeContext: The name of the dbcontext class which we have created above.
  • DESKTOP: Name of the machine to which you are connecting for local you may use dot(.)
  • Practice: Name of the database to which you are connecting.
<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
  </configSections>
  <connectionStrings>
    <add name="PracticeContext" connectionString="Data Source=DESKTOP;Initial Catalog=Practice;User ID=xxx;Password=xxx" providerName="System.Data.SqlClient" />
  </connectionStrings>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
  </startup>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
      <parameters>
        <parameter value="mssqllocaldb" />
      </parameters>
    </defaultConnectionFactory>
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
    </providers>
  </entityFramework>
</configuration>

Output:


Problem

The problem with the above code is that when ever we add any properties to the Student Model the below error will be thrown.

The model backing the PracticeContext context has changed since the database was created. Consider using Code First Migrations to update the database (http://go.microsoft.com/fwlink/?LinkId=238269).

To resolve this we can choose either of these two:
  • We need to add the code as below to the Main Method in Program.cs
    • This approach will delete everything from the database and recreate everything again from the scratch.
     static void Main(string[] args)
    {
      try
      {
        Database.SetInitializer(new DropCreateDatabaseIfModelChanges<PracticeContext>())
        DataBaseCall.GetStudents();
      }
      catch(Exception ex)
      {
        Console.WriteLine("Error occured while performing student operation.", ex);
      }
      Console.ReadKey();
    }
  • Or, We can do as below: This approach will only update the database with the latest changes without impacting the existing things.
    •  Run the command in Package Manager Console Enable-Migrations
      • This command has added a Migrations folder to our project. This new folder contains two files:
        • The Configuration class (Configuration.cs)
        • An InitialCreate migration
    • We need to add the code as below to the Main Method in Program.cs
    static void Main(string[] args)
    {
      try
      {
        Database.SetInitializer(new MigrateDatabaseToLatestVersion<PracticeContext, Configuration>());
        DataBaseCall.GetStudents();
      }
      catch(Exception ex)
      {
        Console.WriteLine("Error occured while performing student operation.", ex);
      }
      Console.ReadKey();
    }

Configuration.cs

The file code looks as below:

    internal sealed class Configuration : DbMigrationsConfiguration<DotnetConsole.PracticeContext>
    {
        public Configuration()
        {
            AutomaticMigrationsEnabled = true;
        }
        protected override void Seed(DotnetConsole.PracticeContext context)
        {
            //  This method will be called after migrating to the latest version.
            //  You can use the DbSet<T>.AddOrUpdate() helper extension method
            //  to avoid creating duplicate seed data.
        }
    }


Do it with async and await

  • Repository.cs
  public class Repository<T>: IDisposable where T: class
  {
    private PracticeContext context =  null;

    public DbSet<T> Entities => this.context.Set<T>();

    public IQueryable<T> Table => this.Entities;

    public Repository(PracticeContext context)
    {
      this.context = context;
    }

    public async Task<List<T>> InsertListAsync(List<T> entites)
    {
      try
      {
        List<T> t = this.Entities.AddRange(entites).ToList();
        await this.context.SaveChangesAsync();
        return t;
      }
      catch (DbEntityValidationException dbEx)
      {
        var msg = string.Empty;
        foreach (var validationErrors in dbEx.EntityValidationErrors)
        {
          foreach (var validationError in validationErrors.ValidationErrors)
          {
            msg += string.Format("Property: {0} Error: {1}{2}", validationError.PropertyName, validationError.ErrorMessage, Environment.NewLine);
          }
        }
        var fail = new Exception(msg, dbEx);
        //Debug.WriteLine(fail.Message, fail);
        throw fail;
      }
    }

    public void Dispose()
    {
      GC.SuppressFinalize(this);
    }
}

  • DataBaseCall.cs
    public static async Task<List<T>> InsertListAsync<T>(List<T> list) where T: class
    {
      try
      {
        return await ExecuteAsync<T, List<T>>(async (repository) =>
        {
          return await repository.InsertListAsync(list);
        });
      }
      catch (Exception ex)
      {
        Console.WriteLine(ex);
        return null;
      }
    }

     private async static Task<TResult> ExecuteAsync<T, TResult>(Func<Repository<T>, Task<TResult>> func) where T : class
    {
      using (PracticeContext context = new PracticeContext())
      {
        try
        {
          return await func(new Repository<T>(context));
        }
        catch (Exception ex)
        {
          Console.WriteLine(ex);
          throw new Exception("Error Occured.");
        }
      }
    }

Program.cs

     static void Main(string[] args)
    {
      DataBaseCallMethod();
      Console.ReadKey();
    }

    private static async void DataBaseCallMethod()
    {
      try
      {
        Database.SetInitializer(new MigrateDatabaseToLatestVersion<PracticeContext, Migrations.Configuration>());
        var students = new List<Student>();
        var results = await DataBaseCall.InsertListAsync(students);
      }
      catch(Exception ex)
      {
        Console.WriteLine("Error occured while performing student operation.", ex.ToString());
      }
    }

Please refer below link for details.

Comments

Popular posts from this blog

All the require docs of job seekers (java docs, testing docs, study materials, genuine consultancy link)

# JUST_FOR  JAVA LOVERS : CORE & ADVANCED JAVA Notes ! Core Java and Advanced Java Notes! . Link 1 :-  https://www.dropbox.com/s/t…/1--%20Core%20Java%28TOC%29.pdf… Link 2 :-  https://www.dropbox.com/s/0ahi8r…/9--%20Advanced%20Java.pdf… _________________________________ 1.  https://drive.google.com/…/0BxJrew1xg5ZLZjdabnVUUkswalk/view 2. https://drive.google.com/…/0BxJrew1xg5ZLVHVSQ0tiU1VmVXM/view 3. https://drive.google.com/…/0BxJrew1xg5ZLdS1JUTAxMWtTcEU/view 4. https://drive.google.com/…/0BxJrew1xg5ZLY1M5bGp2Ym5lZUk/view 5. https://drive.google.com/…/0BxJrew1xg5ZLck13Rndpd1ZlcEU/view 6.  https://drive.google.com/…/0BxJrew1xg5ZLb2xodlhOTEVzLTA/view --------------------- Spring --------------------- https://drive.google.com/…/0BxJrew1xg5ZLQ3VaQnlja3N1Mkk/view https://drive.google.com/…/0BxJrew1xg5ZLTXNVLUdOR0xmTzQ/view https://drive.google.com/…/0BxJrew1xg5ZLWU9PUmVKUUYtMjQ/view ---------------------- Hibernate ---------------------...

Use your mobile phone as web cam to your pc/laptop

 Brief  This thing started when I had an interview😮 with one of the company in Nepal and my laptop camera quality was so bad that I was asked to turn off 😥 it which made me to think to buy a webcam for my laptop and I realized if the webcam be attached to my laptop using USB cable or WIFI then why we cannot use my phone. This is how it started. I have tested it and using this app called iVCam since then it worked very well as well with teams, zoom and other streaming app as well. We need to download these 2 stuffs only: The mobile app. The PC client software. Install these two software on your individual mobile and pc and then you are good to go.Mobile phone will ask for the permissions, and allow all the permission it ask for. There are two by which we can connect your mobile phone as web cam and they are:  A) WIFI: Connect your laptop and mobile to the same wifi network. Open app in both mobile and laptop/pc. It will automatically connect to the mobile camera....

Terminology used in CPU, GPU, APU, RAM or Hardisk(HDD or SSD)

 Brief We will discuss the terminology used in the CPU, RAM or Hardisk(HDD or SSD). CPU GPU Si RAM Virtual Memory (From the  also called Page File,  Single Stick Dual Stick Single Channel Dual Channel Single Slot Dual Slot RAM Disk RAM Caching Note - More size, more frequency, more channel, and less latency is best for RAM but it depends upon your laptop, if your laptop mother board only supports 2400MHz frequency then buying more than that frequency of RAM is not good idea because your RAM will work at 2400MHz frequency only. If you want to use the High Frequency RAM then use the technique of RAM Disk and RAM Caching. Hardisk References & Recommendations 🔥 High Speed RAM 🔥 Does Memory Frequency Matter? 🔥Desktop vs Laptop 🔥 Hindi