【发布时间】:2017-01-27 18:47:20
【问题描述】:
刚开始学习 .NET Core。我设法将Migration folder、ApplicationDbContext 和ApplicationUser 文件移动到.net 核心类库项目并将其引用到Web 项目。这工作正常,因为我可以在我的数据库中看到与用户角色和声明相关的默认 7 个表。
现在我有类似的模型
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace Domain.Model
{
[Table("Employee")]
public class Employee
{
[Key]
public int EmployeeId{get; set;}
[Required, MaxLength(100)]
public string Name {get;set;}
}
}
在 ApplicationDbContext 文件中
namespace BLL
{
public class ApplicationDbContext : IdentityDbContext<ApplicationUser, IdentityRole<int>, int>
{
public DbSet<Employee> Employees {get;set;}
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options): base(options)
{
}
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
}
}
}
然后我创建了控制器类 EmployeeController 并在 Index 方法中刚刚创建了 Employee 的新对象
public class EmployeeController : Controller
{
public IActionResult Index()
{
Employee e = new Employee();
return View();
}
}
然后我使用索引视图运行一个项目,但这并没有在我的数据库中创建 Employee 表。
我已经参考了这些文章
更改 aspnet 用户表的主键数据类型
https://medium.com/@goodealsnow/asp-net-core-identity-3-0-6018fc151b4#.fxdzevyzn
.NET Core 中的 CRUD 操作
https://docs.microsoft.com/en-us/aspnet/core/tutorials/first-mvc-app/adding-model
添加controller with views, using Entity Framework 时出现错误,因此我必须创建空控制器。
我应该如何创建将我的模型注入 ApplicationDbContext 或生成迁移文件以更新数据库?
【问题讨论】:
-
检查您的员工注释:MaxLenght 可能需要 -> MaxLength。
标签: c# asp.net asp.net-mvc asp.net-core asp.net-core-mvc