The reason for this was that the RedditContext class was not tracking any of the objects nor where the relationships specified on the OnModelCreating.
So I've modified the RedditContext class
using Microsoft.Data.Entity; namespace Reddit.Models { public class RedditContext : DbContext { public RedditContext() { //Database.EnsureCreated(); } public DbSet<RedditUser> RedditUsers { get; set; } public DbSet<UserDetails> UserDetails { get; set; } public DbSet<SubReddit> SubReddits { get; set; } public DbSet<Post> Posts { get; set; } public DbSet<Comment> Comments { get; set; } protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { var connection = Startup.Configuration["Data:RedditContextConnection"]; optionsBuilder.UseSqlServer(connection); base.OnConfiguring(optionsBuilder); } protected override void OnModelCreating(ModelBuilder modelBuilder) { modelBuilder.Entity<RedditUser>() .HasKey(x => x.RedditUserId); modelBuilder.Entity<UserDetails>() .HasKey(x => x.UserId); modelBuilder.Entity<RedditUser>() .HasOne(x => x.UserDetails) .WithOne(x => x.User); base.OnModelCreating(modelBuilder); } } }
In order to recover from this mistake, we want to apply the new migration, but the Core migration contains nothing so I will first unapply it
dnx ef database update Start
dnx ef migrations remove
dnx ef migrations add Core
dnx ef database update
This time it worked .... hurray
At this point, it's worth considering how will this be deployed to a test environment and the answer is pretty simple. A migration script can be created with the following command:
dnx ef migrations script
This creates a Sql script that can be used to create the database.
It's time to have another look at our repository
Let's start with the IRedditRepository, where we now have a few methods and this time we'll try to implement some of them.using System.Collections.Generic; namespace Reddit.Models { public interface IRedditRepository { UserDetails GetUserDetails(); IEnumerable<SubReddit> GetSubscribedSubReddits(RedditUser user); IEnumerable<SubReddit> GetSubReddits(); IEnumerable<Post> GetSubRedditPosts(SubReddit subreddit); IEnumerable<Comment> GetPostComments(Post post); } }
using Microsoft.Extensions.Logging; using System; using System.Collections.Generic; using System.Linq; namespace Reddit.Models { public class RedditRepository : IRedditRepository { private readonly RedditContext ctx; private readonly ILogger<RedditRepository> logger; public RedditRepository(RedditContext ctx, ILogger<RedditRepository> logger) { this.ctx = ctx; this.logger = logger; } public IEnumerable<Comment> GetPostComments(Post post) { return ctx.Posts .Where(x => x.PostId == post.PostId) .Single() .Comments; } public IEnumerable<Post> GetSubRedditPosts(SubReddit subreddit) { return ctx.SubReddits .Where(x => x.SubRedditId == subreddit.SubRedditId) .Single() .Posts; } public IEnumerable<SubReddit> GetSubReddits() { return ctx.SubReddits.AsEnumerable(); } public IEnumerable<SubReddit> GetSubscribedSubReddits(RedditUser user) { throw new NotImplementedException(); } public UserDetails GetUserDetails() { throw new NotImplementedException(); } } }
Straight away we see that there are going to be problems as we lack paging capabilities, but we'll add those later
In any case, we'll need to expose these method somehow, there are potentially two options here depending on what we're trying to accomplish:
We could use these methods from inside a Controller or We could create an API and we're definitely going for the latter as it's way cooler
Create a new folder called Api inside the Controllers folder and add a new Web API controller class, called SubRedditController
using System.Collections.Generic; using Microsoft.AspNet.Mvc; using Reddit.Models; // For more information on enabling Web API for empty projects, visit http://go.microsoft.com/fwlink/?LinkID=397860 namespace Reddit.Controllers.Api { [Route("api/[controller]")] public class SubRedditController : Controller { private readonly IRedditRepository repository; public SubRedditController(IRedditRepository repository) { this.repository = repository; } // GET: api/values [HttpGet] public IEnumerable<SubReddit> Get() { return repository.GetSubReddits(); } } }
Really great article . i like this and it is very useful .
ReplyDeleteSalesforce Training in Chennai | Certification | Online Course | Salesforce Training in Bangalore | Certification | Online Course | Salesforce Training in Hyderabad | Certification | Online Course | Salesforce Training in Pune | Certification | Online Course | Salesforce Online Training | Salesforce Training
Informative blog
ReplyDeleteThis article explains to you the Scope of Digital Marketing in India 2020 and the career opportunities on Digital Marketing.
ReplyDeleteThanks for sharing. Nice post. SAP Training in Gurgaon
ReplyDeleteAt Digital Marketing Thanks for this amazing content.
ReplyDeleteTableau training institute in Hyderabad can be a ticket to your fortunate tableau career. Dashboards, tableau desktop, Tableau online,charts,and many other key features of tableau this is best for you have interest visit my website link http://tableautrainings.in/
ReplyDeleteReact JS Training in Hyderabad
ReplyDeleteThanks for sharing this amazing post.
ReplyDeleteNice thanks for sharing informative post like this keep posting if like more details visit my website linkht
ReplyDeleteNice article , Thanks for sharing your knowledge with us
ReplyDeleteJava training with real-time projects
this is very useful content thanks for sharing
ReplyDeletetesting tools online training in hyderabad
Thank you for sharing this Blog, This is more informative and nice.
ReplyDeleteAWS Job Oriented Training in Hyderabad
Nice thanks for sharing informative.
ReplyDeleteThanks for sharing the information about musing on information technology, it is very useful content keep posting more
ReplyDeleteReact JS training institute in KPHB
Thanks for sharing this amazing post.
ReplyDeletepowerbi training in hyderabad