I use dotnet aspnet-codegenerator controller to create default views an controller for a model in area
My command: dotnet aspnet-codegenerator controller -name CartController -namespace AppMvc.Net.Areas.Cart.Controllers -udl -dc AppMvc.Net.Models.AppDbContext -outDir Areas/Cart/Controllers -m AppMvc.Net.Models.Cart.CartModel
When I run it: There is an error:
Exception of type ‘System.InvalidOperationException’ was thrown.
at Microsoft.VisualStudio.Web.CodeGeneration.ActionInvoker.b__6_0()
at Microsoft.Extensions.CommandLineUtils.CommandLineApplication.Execute(String[] args)
at Microsoft.VisualStudio.Web.CodeGeneration.ActionInvoker.Execute(String[] args)
at Microsoft.VisualStudio.Web.CodeGeneration.CodeGenCommand.Execute(String[] args)
My Code:
AppDbContext:
`
using AppMvc.Net.Models.Blog;
using AppMvc.Net.Models.Cart;
using AppMvc.Net.Models.Product;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
using Microsoft.VisualStudio.Web.CodeGenerators.Mvc.Templates.General;
namespace AppMvc.Net.Models
{
public class AppDbContext : IdentityDbContext<AppUser>
{
public AppDbContext(DbContextOptions<AppDbContext> options) : base(options)
{
// Phương thức khởi tạo này chứa options để kết nối đến MS SQL Server
// Thực hiện điều này khi Inject trong dịch vụ hệ thống
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
base.OnConfiguring(optionsBuilder);
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
foreach (var entityType in modelBuilder.Model.GetEntityTypes())
{
var tableName = entityType.GetTableName();
if (tableName.StartsWith("AspNet"))
{
entityType.SetTableName(tableName.Substring(6));
}
}
modelBuilder.Entity<Category>(entity =>
{
entity.HasIndex(c => c.Slug).IsUnique();
});
modelBuilder.Entity<PostCategory>(entity =>
{
entity.HasKey(c => new { c.PostID, c.CategoryID });
});
modelBuilder.Entity<Post>(entity =>
{
entity.HasIndex(p => p.Slug)
.IsUnique();
});
modelBuilder.Entity<CategoryProduct>(entity =>
{
entity.HasIndex(c => c.Slug).IsUnique();
});
modelBuilder.Entity<ProductCategoryProduct>(entity =>
{
entity.HasKey(c => new { c.ProductID, c.CategoryID });
});
modelBuilder.Entity<ProductModel>(entity =>
{
entity.HasIndex(p => p.Slug)
.IsUnique();
});
}
public DbSet<Contact> Contacts { get; set; }
public DbSet<Category> Categories { get; set; }
public DbSet<Post> Posts { get; set; }
public DbSet<PostCategory> PostCategories { get; set; }
public DbSet<CategoryProduct> CategoryProducts { get; set; }
public DbSet<ProductModel> Products { get; set; }
public DbSet<ProductCategoryProduct> ProductCategoryProducts { get; set; }
public DbSet<ProductPhoto> ProductPhotos { get; set; }
// Cart-related DbSets
public DbSet<CartModel> Carts { get; set; }
public DbSet<CartItem> CartItems { get; set; }
}
}
CartModel:
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace AppMvc.Net.Models.Cart
{
[Table("Cart")]
public class CartModel
{
[Key]
public int CartId { get; set; }
[Required]
public string UserId { get; set; }
[ForeignKey("Id")]
public AppUser User { get; set; }
public List<CartItem> CartItems { get; set; } = new List<CartItem>();
[NotMapped]
public decimal TotalPrices
{
get
{
decimal total = 0;
foreach (var item in CartItems)
{
total += item.TotalPrice;
}
return total;
}
}
}
}```
I inherit IdentityDbContext<AppUser> in appDbContext instead of DbContex
this error appear when I use IdentityDbContext<AppUser>
Dia is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.