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 build 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# |
コードのコピー
|
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# |
コードのコピー
|
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 to the mapped classes, as demonstrated in the following table:
Note: For LINQ queries, the code must have declared "using System.Linq".
INSERT |
C# |
コードのコピー
|
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);
context.SaveChanges();
Console.WriteLine("New book added successfully! \n\n");
}
|
|
SELECT |
C# |
コードのコピー
|
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# |
コードのコピー
|
public static void Update(BookContext context)
{
Console.WriteLine("Updating book... ");
// 更新する必要がある本を取得します。
Books book = (
from p in context.Books
where p.Id == 100
select p
).FirstOrDefault();
// 必要なプロパティを更新します。
book.Location_City = "London";
context.SaveChanges();
Console.WriteLine("Update successful! \n\n");
}
|
|
DELETE |
C# |
コードのコピー
|
public static void Delete(BookContext context)
{
Console.WriteLine("Deleting book...");
// 削除する本を定義します。
var book = context.Books.Last();
// 本を削除し、データソースへの変更を保存します。
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 the creation of the model in the Entity Framework.