fire_water
fire_water

Reputation: 1449

Models/IdentityModel.cs missing

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

My development environment:

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?

Upvotes: 6

Views: 9373

Answers (3)

You probably selected "No Authentication" and that is why you do not have the "Models/IdentityModel.cs" file

Upvotes: 0

laika
laika

Reputation: 1497

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.

Upvotes: 2

Marcelo
Marcelo

Reputation: 4282

The warning is for when you selected other than No Authentication for authentication method.

In case you selected Individual User Account, a new Entity Framwork model will be create for the use database.

You probably selected "No Authentication" and that is why you do not have the "Models/IdentityModel.cs" file

Upvotes: 0

Related Questions