In This Topic
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:
C# |
Copy Code |
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.
C# |
Copy Code |
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 can be used to perform various data operations on the mapped classes, as demonstrated in the following table:
Note: For LINQ queries, the code must have declared "using System.Linq".
INSERT |
C# |
Copy Code |
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 |
C# |
Copy 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);
}
} | |
UPDATE |
C# |
Copy Code |
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 |
C# |
Copy Code |
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");
} | |
Note: For all dataconnectors, the
Scaffolding feature supports the creation of the model and the dbcontext along with the creation of the model in the Entity Framework.