# Entity Framework

## Content

The ADO.NET Provider for OData supports Entity Framework through the **C1.EntityFrameworkCore.OData** package, which needs to be installed in addition. This article demonstrates the model-first approach to building an Entity Framework model that maps data tables to classes for simpler access to OData.

The code example below defines the **Books** class as mapping the Books datatable:

```csharp
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; }
}
```

The next code example defines **BookContext** class to access the **Books** datatable. This is done by overriding the **OnConfiguring** method, which invokes the **UseOData** method of the passed **DbContextOptionsBuilder** object, to configure the context and establish a connection to the OData service.

```csharp
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}");
}
```

Now the [LINQ queries](/componentone/docs/services/online-dataconnector/ado.net-provider-for-dynamics-365-sales/D365linqqueries) can be used to perform various data operations on the mapped classes, as demonstrated in the following table:

>type=note
> **Note**: For LINQ queries, the code must have declared "**using** System.Linq".

**INSERT**

```csharp
public static void Insert(BookContext context)
{
    Console.WriteLine("Adding new book...");

    // Create a 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";

    // Add book and save changes to the data source
    context.Books.Add(book);
    context.SaveChanges();

    Console.WriteLine("New book added successfully! \n\n");
}
```

**SELECT**

```csharp
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);
    }
}
```

**UPDATE**

```csharp
public static void Update(BookContext context)
{
    Console.WriteLine("Updating book... ");

    // Retrieve the book that needs to be updated
    Books book = (
            from p in context.Books
            where p.Id == 100
            select p
        ).FirstOrDefault();

    // Update the required property
    book.Location_City = "London";
    context.SaveChanges();

    Console.WriteLine("Update successful! \n\n");
}
```

**DELETE**

```csharp
public static void Delete(BookContext context)
{
    Console.WriteLine("Deleting book...");

    // Define the book to be deleted
    var book = context.Books.Last();

    // Delete the book and save changes to the data source
    context.Books.Remove(book);
    context.SaveChanges();

    Console.Write("Book deleted: " + book.ISBN + "\n");
}
```

>type=note
> **Note**: For all dataconnectors, the **[Scaffolding](/componentone/docs/services/online-dataconnector/scaffolding)** feature supports the creation of the model and the dbcontext along with the creation of the model in the Entity Framework.