主要解决黑窗口的报错
users这个类有一个邀请人属性,为什么这个能直接映射到数据库,而article不行,
试过在next属性上加ForeignKey特性也不行
最好是使用Flunt Api指定一下。
我测试的效果:
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Design;
using Microsoft.Extensions.Configuration;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
namespace csblog_133043
{
public class CnBlog133043MigrationsContextFactory : IDesignTimeDbContextFactory<CnBlog133043Context>
{
public CnBlog133043Context CreateDbContext(string[] args)
{
var configuration = BuildConfiguration();
var builder = new DbContextOptionsBuilder<CnBlog133043Context>()
.UseSqlServer(configuration.GetConnectionString("Default"));
return new CnBlog133043Context(builder.Options);
}
private static IConfigurationRoot BuildConfiguration()
{
var builder = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", optional: false);
return builder.Build();
}
}
}
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Text;
using Volo.Abp.Data;
using Volo.Abp.EntityFrameworkCore;
using Volo.Abp.EntityFrameworkCore.Modeling;
namespace csblog_133043
{
[ConnectionStringName("Default")]
public class CnBlog133043Context : AbpDbContext<CnBlog133043Context>
{
public CnBlog133043Context(DbContextOptions<CnBlog133043Context> options)
: base(options)
{
}
public DbSet<Article> Articles { get; set; }
public DbSet<Users> Users { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Users>(u=>
{
u.ToTable(nameof(Users));
u.ConfigureByConvention();
u.HasOne(n => n.Referrer).WithOne();
});
modelBuilder.Entity<Article>(a=>
{
a.ToTable(nameof(Article));
a.ConfigureByConvention();
a.HasOne(n => n.Next).WithOne();
a.HasOne(n => n.Previous).WithOne();
});
}
using System;
using System.Collections.Generic;
using System.Text;
namespace csblog_133043
{
public class Users
{
public int Id { get; set; }
public Users Referrer { get; set; }
}
}
using System;
using System.Collections.Generic;
using System.Text;
namespace csblog_133043
{
public class Article
{
public int Id { get; set; }
public Article Next { get; set; }
public Article Previous { get; set; }
}
}
{
"ConnectionStrings": {
"Default": "Data Source=(localdb)\\MSSQLLocalDB;Initial Catalog=cnblog;Integrated Security=True"
}
}
我已经解决了,还是谢谢你,我的结构,dbset用的是泛型。
@万分感谢: Context不添加DbSet<TEntity>能行吗? 你后面这个只是一个仓储操作,与Migration无关。
@Jonny-Xhl: 我没有用Migration,我是用的DbFactory