Asp.net Core-IdentityDbContext不创建其他表,仅创建标识表

时间:2019-03-06 12:28:29

标签: c# entity-framework asp.net-identity

我正在使用Add-Migration Initial创建数据库,但是它仅创建Identity表,而不创建表Categoria。任何想法我在做什么错?

using Ecommerce.Core;
using Ecommerce.Core.Domain.Categorias;
using Ecommerce.Core.Domain.Roles;
using Ecommerce.Core.Domain.Usuarios;
using Ecommerce.Data.ModelExtensions;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;

namespace Ecommerce.Data
{
    public class EcommerceContext : IdentityDbContext<Usuario, Role, int>
    {
        public EcommerceContext(DbContextOptions<EcommerceContext> options) : base(options) { }

        protected override void OnModelCreating(ModelBuilder builder)
        {
            base.OnModelCreating(builder);
            //builder.AddEntityConfigurationsFromAssembly(GetType().Assembly);
            builder.Entity<Categoria>(c =>
            {
                c.ToTable("Categoria");
                c.HasKey(i => i.Id);
                c.Property(i => i.Nome);
                c.Property(i => i.dataCadastro);
            });
        }

        public DbSet<Categoria> Categoria { get; set; }
    }
}

1 个答案:

答案 0 :(得分:0)

using Ecommerce.Core;
using Ecommerce.Core.Domain.Categorias;
using Ecommerce.Core.Domain.Roles;
using Ecommerce.Core.Domain.Usuarios;
using Ecommerce.Data.ModelExtensions;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;

namespace Ecommerce.Data
{
    public class EcommerceContext : IdentityDbContext<Usuario, Role, int>
    {
        public DbSet<Categoria> Categoria { get; set; }

        public EcommerceContext(DbContextOptions<EcommerceContext> options) : base(options) { }

        protected override void OnModelCreating(ModelBuilder builder)
        {
            //builder.AddEntityConfigurationsFromAssembly(GetType().Assembly);
            builder.Entity<Categoria>(c =>
            {
                c.ToTable("Categoria");
                c.HasKey(i => i.Id);
                c.Property(i => i.Nome);
                c.Property(i => i.dataCadastro);
            });

            base.OnModelCreating(builder);
        }
    }
}

要使更改生效的实体配置后,您必须致电base.OnModelCreating(builder);

对不起,我的英语,希望对您有所帮助。