DataConnector
ADO.NET provider for OData / Entity Framework
In This Topic
    Entity Framework
    In This Topic

    The ADO.NET Provider for OData supports Entity Framework which requires C1.EntityFrameworkCore.OData package to be installed. In this section, we have used the model first approach to build an Entity Framework model that maps data tables to classes for simpler access to OData.

    The below code example defines the Books class to map the Books datatable.

    public class Books
     {
       [Key]
       [DataMember(Name = "Id")]
       public int Id { get; set; }
       [DataMember(Name = "ISBN")]
       public String ISBN { get; set; }
       [DataMember(Name = "Title")]
       public String Title { get; set; }
       [DataMember(Name = "Author")]
       public String Author { get; set; }
       [DataMember(Name = "Price")]
       public Double Price { get; set; }
       [DataMember(Name = "Location_Street")]
       public String Location_Street { get; set; }
       [DataMember(Name = "Location_City")]
       public String Location_City { get; set; }
    }
    

    Now, BookContext class has been defined to access the Books datatable and establish a connection to the OData service by overriding the OnConfiguring method. This method invokes the UseOData method of DbContextOptionsBuilder class to configure the context and establish connection with OData service.

    public DbSet<Books> Books { get; set; }
    public BookContext() : base()
    {
      Database.AutoTransactionsEnabled = false;
    }
    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
      const string SampleUrl = @"http://XX.XX.X.XXX/ODataServer/odata;Password=******;Username=*****";
    optionsBuilder.UseOData($@"Url={SampleUrl}");
    }
    

    You can now use the LINQ queries to perform different data operations to the mapped classes as demonstrated in the code below. Note that "using System.Linq" must be declared in your code.

    public static void SelectLinq(BookContext context)
    {
       Console.WriteLine("Displaying Books Present in our collection...");
       var records = from p in context.Books
                          select p;
       foreach (var book in records)
       {
          Console.WriteLine("{0} - {1} -{2} -{3}", book.Id,book.ISBN,book.Title,book.Location_City);
       }
    }
    
    public static void Insert(BookContext context)
    {
       Console.WriteLine("Adding New Book...");
       Books book = new Books();
       book.Id = 2000;
       book.ISBN = "100-20-70";
       book.Title = "Art of Exploring--New Concept";
       book.Location_City="Delhi";
       book.Location_Street = "Nai Sadak";
       book.Price = 500.00;
       book.Author = "Chetan Kothari";
       context.Books.Add(book);//Adding book
       context.SaveChanges();//Save changes to the data source
       Console.WriteLine("New book added successfully.. \n \n");
    }
    
    public static void Delete(BookContext context)
    {
    
       Console.WriteLine("Delete Started...");
       var book = context.Books.Last();
       context.Books.Remove(book);//Deleting book
       context.SaveChanges();//Save changes to the data source
       Console.WriteLine("Deleted Successful... \n \n ");
    }
    
    public static void Update(BookContext context)
    {
       Console.WriteLine("Update Started... ");
       books book = (from p in context.Books
                   where p.Id == 100
                   select p).FirstOrDefault(); //Retriving book that need to be update
       book.Location_City = "London";//Updated the required property or attribute
       context.SaveChanges();
       Console.WriteLine("Update Successful...\n \n ");
    }
    
    Note: Scaffolding feature supports user to create the model and dbcontext when you create model in the Entity Framework for all dataconnectors.