Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using Microsoft.EntityFrameworkCore;
- using System.Collections.Generic;
- namespace Example
- {
- /**
- * DbContext classes provide implicit access to a database through the LINQ query syntax and the EntityFrameworkCore ORM.
- */
- public class ExampleDataProvider : DbContext
- {
- /**
- * The schema is defined implicitly through the member variables of the DbContext.
- * Each DbSet provides transparent access to a table in the database through LINQ.
- * The schema is initialised automatically by the DbContext base class where necessary.
- * The columns of each table are defined by the member variables of the POCOs each DbSet holds.
- */
- public DbSet<Blog> Blogs { get; set; }
- public DbSet<Post> Posts { get; set; }
- private int NextBlogId {
- get {
- // Defining a custom getter so that NextBlogId will increment every time it is accessed.
- return NextBlogId++;
- }
- } = 0; // No, this isn't C++, that doesn't make it abstract.
- // This is essentially the same as setting NextBlogId to 0 in the initialiser list of the constructor.
- private int NextPostId {
- get {
- return NextPostId++;
- }
- } = 0;
- /**
- * This method can be overridden to configure the DbContext when it is constructed.
- * By selecting options with the optionsBuilder we choose various parameters for our database,
- * including what type of database it is. .NET Core 2.0 supports MSSQL, SQLite, and a pure in-memory database.
- * Note that no matter what database provider we choose, the query syntax is still the same.
- * This means that when Microsoft finally get around to implementing NoSQL databse providers,
- * a DbContext can be configured to use them by changing only a single line of code.
- */
- protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
- {
- // The database file will automatically be created
- // and initialised with the schema defined above
- // if it does not already exist.
- optionsBuilder.UseSqlite("Data Source=ExampleDatabase.db");
- }
- /**
- * This method performs operations on our database.
- * Methods like these can be consumed by a Controller class to implement a WebAPI,
- * although obviously methods in a real DbContext would strive to be more generically useful.
- */
- void AddBlog(String blogUrl)
- {
- // We write to the database by using the LINQ methods and POCOs.
- // POCOs can be specified with similar syntax to JSON objects.
- Blogs.Add(new Blog {
- BlogId = NextBlogId,
- Url = blogUrl
- });
- // All LINQ operations involving write operations are implicitly transactional.
- // The transaction begins with the first write call and and doesn't finalise
- // until we call SaveChanges(). This means that methods on DbContexts are thread
- // safe, so we can use them asynchronously with Tasks and 'await'.
- SaveChanges();
- }
- void AddPost(String title, String content, String postUrl)
- {
- Blog authorBlog = Blogs
- .Where(b => postUrl.startsWith(b.Url))
- .GetFirstOrDefaultAsync();
- var newPost = new Post{
- PostId = NextPostId,
- Title = title,
- Content = content,
- BlogId = authorBlog.BlogId
- };
- Posts.Add(newPost);
- // Yes, this actually updates authorBlog in the database!
- authorBlog.Posts.Add(newPost);
- SaveChanges();
- }
- }
- /**
- * These are POCOs (Plain Ole C# Objects).
- * POCOs are C# classes consisting entirely of public member variables with automatic
- * getters and setters.
- * They form the in-code representation of the rows in our database.
- *
- * Rows can be retrieved from the database by using LINQ queries on DbSets of POCOs,
- * or inserted into the database by constructing a POCO and adding it to the DbSet.
- *
- * POCOs can be implicitly serialised to JSON and sent as the response to GET requests
- * using JSON WebAPI Controllers. They just need to be wrapped in an ObjectResult object.
- */
- public class Blog
- {
- /**
- * Optionally, JSON.NET attributes can be added to the members of a POCO to change how it
- * is serialised. The property below ensures that BlogId will always be the first member
- * of the serialised object.
- *
- * Properties can also be used to change the names of members as they appear in JSON,
- * specify if fields are optional or required, and specify what to write to the JSON
- * if an optional field is null.
- */
- [JsonProperty(Order = -2)]
- public int BlogId { get; set; }
- public string Url { get; set; }
- public int Rating { get; set; }
- public List<Post> Posts { get; set; }
- }
- public class Post
- {
- public int PostId { get; set; }
- public string Title { get; set; }
- public string Content { get; set; }
- public int BlogId { get; set; }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement