Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Models/IdentityModel.cs missing

I am new to .NET and currently learning ASP.NET Core MVC by following this tutorial.

My development environment:

  • Ubuntu 16.04.1 LTS
  • .Net Core 1.0.0-preview2-1-003177
  • Visual Studio Code 1.7.2
  • generator-aspnet 0.2.5 (creates project scaffolding)

So, I created a new project using yo aspnet and selected Web Application, which includes Individual User Accounts-based authentication. In the tutorial, I'm now at the "Create your model" section and feeling confused on how to proceed.

Specifically, the model the tutorial asks you to create looks like this:

Models/Model.cs:

using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;

namespace EFGetStarted.AspNetCore.NewDb.Models
{
    public class BloggingContext : DbContext
    {
        public BloggingContext(DbContextOptions<BloggingContext> options)
            : base(options)
        { }

        public DbSet<Blog> Blogs { get; set; }
        public DbSet<Post> Posts { get; set; }
    }

    public class Blog
    {
        public int BlogId { get; set; }
        public string Url { 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; }
        public Blog Blog { get; set; }
    }
}

However, on that page, there's a warning:

If you use Individual User Accounts instead of None for Authentication then an Entity Framework model will be added to your project in Models\IdentityModel.cs. Using the techniques you will learn in this walkthrough, you can choose to add a second model, or extend this existing model to contain your entity classes.

My confusion started when I could not find the file they reference, Models/IdentityModel.cs. I do see Models/ApplicationUser.cs, which looks like this:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;

namespace EFGetStarted.AspNetCore.NewDb.Models
{
    // Add profile data for application users by adding properties to the ApplicationUser class
    public class ApplicationUser : IdentityUser
    {
    }
}

... and Data/ApplicationDBContext.cs, which looks like this:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
using EFGetStarted.AspNetCore.NewDb.Models;
namespace EFGetStarted.AspNetCore.NewDb.Data
{
    public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
    {
        public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
            : base(options)
        {
        }
        protected override void OnModelCreating(ModelBuilder builder)
        {
            base.OnModelCreating(builder);
            // Customize the ASP.NET Identity model and override the defaults if needed.
            // For example, you can rename the ASP.NET Identity table names and more.
            // Add your customizations after calling base.OnModelCreating(builder);
        }
    }
}

The warning message above has me confused on how to proceed. Since Models/IdentityModel.cs does not exist, do I place the contents of Model.cs into Models/ApplicationUser.cs or Data/ApplicationDBContext.cs or do I leave everything as is?

like image 340
fire_water Avatar asked Dec 06 '16 19:12

fire_water


1 Answers

I could not find the file they reference, Models/IdentityModel.cs.

generator-aspnet is using different version of templates than Visual C# Web Template shipped with Visual Studio 2015.

File Models/IdentityModel.cs should contain classes ApplicationUser and ApplicationDbContext. They are the same as in the version used by generator-aspnet.

do I place the contents of Model.cs into Models/ApplicationUser.cs or Data/ApplicationDBContext.cs or do I leave everything as is

You can leave it as it is - it does not matter where are these files located.

like image 61
laika Avatar answered Oct 16 '22 17:10

laika