mr-*_*taj 3 c# asp.net asp.net-core
我需要在 ASP.NET Core 3.0 中使用代码优先创建一个数据库
这是DbContext:
public class TrelloContext : DbContext
{
public TrelloContext(DbContextOptions options) : base(options)
{
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.AddDbSet<IEntity>(typeof(IEntity).Assembly);
modelBuilder.ApplyConfigurationsFromAssembly(typeof(IType).Assembly);
}
}
Run Code Online (Sandbox Code Playgroud)
这是启动:
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers().AddControllersAsServices();
services.AddDbContext<TrelloContext>(options => options.UseSqlServer(Configuration.GetConnectionString("SqlServer")));
services.Injection();
}
Run Code Online (Sandbox Code Playgroud)
这是我的连接字符串:
"ConnectionStrings": {
"SqlServer": "Data Source=.;Initial Catalog=TrelloDB;Trusted_Connection=True;Trusted_Connection=True;"
}
Run Code Online (Sandbox Code Playgroud)
当我使用这个时add-migration initial,我收到此错误:
值不能为空。(参数“键”)
有什么问题?我该如何解决这个问题?
这是因为您的实体之一(继承自接口:)IEntity没有标识列。
请检查您的所有实体。确保他们都有一个 ID 或标记为 的属性[Key]。
public class MyEntity : IEntity
{
// make sure:
public int Id { get; set; }
// or:
[Key]
public int SomeProperty { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
14243 次 |
| 最近记录: |