我正在尝试使用我.Net core console app
和我的本地MySQL
使用entity framework core
Code first
方法中的模型创建迁移,一切都很好,但是在创建迁移并尝试使用之后,update-database
我在NugetPackage Console
楷模:
领域
class Branche
{
[Key]
public string ID { get; set; }
[Required]
public string Title { get; set; }
public ICollection<Student> Students { get; set; }
}
学生
class Student
{
[Key]
public string ID { get; set; }
public string Name { get; set; }
public string BrancheID { get; set; }
public Branche Branche { get; set; }
}
数据库上下文:
class SchoolContext:DbContext
{
public SchoolContext()
{
}
public SchoolContext(DbContextOptions<SchoolContext> dbContextOptions):base(dbContextOptions)
{
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
// Map entities to tables
modelBuilder.Entity<Branche>().ToTable("Branche");
modelBuilder.Entity<Student>().ToTable("Student");
// Configure Primary Keys
modelBuilder.Entity<Branche>().HasKey(b => b.ID).HasName("PK_Branche");
modelBuilder.Entity<Student>().HasKey(s => s.ID).HasName("PK_Student");
//Configure Relations
modelBuilder.Entity<Student>().HasOne<Branche>().WithMany().HasPrincipalKey(b => b.ID)
.HasForeignKey(s => s.BrancheID).OnDelete(DeleteBehavior.NoAction)
.HasConstraintName("FK_Student_Branche");
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
IConfiguration configuration = new ConfigurationBuilder()
.SetBasePath(Directory.GetParent(AppContext.BaseDirectory).FullName)
.AddJsonFile("appsettings.json", false).Build();
optionsBuilder.UseMySql(configuration.GetConnectionString("MySQL"), ServerVersion.AutoDetect(configuration.GetConnectionString("MySQL")));
}
public DbSet<Branche> Branches { get; set; }
public DbSet<Student> Students { get; set; }
}
错误:
Failed executing DbCommand (3ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
CREATE TABLE `Branches` (
`ID` nvarchar(450) NOT NULL,
`Title` nvarchar(max) NOT NULL,
CONSTRAINT `PK_Branches` PRIMARY KEY (`ID`)
);
我使用Pomelo.EntityFrameworkCore.MySql 5.0.0-alpha.2
那么请对此问题有任何帮助吗?