Создал две модели и контекст данных. Сделал миграцию(Add-Migration Initial) и выполнил команду создания БД (Update-Database), БД создалась, но при её открытии вылетает ошибка. Вопрос, почему, всю голову себе сломал, не могу нормально работать с SQLite и EF.
Вот модели и контекст:
class Phone
{
public int Id { get; set; }
public string Name { get; set; }
public string Title { get; set; }
[ForeignKey("CompanyId")]
public virtual Company Company { get; set; }
public Phone(string Name, string Title)
{
this.Name = Name;
this.Title = Title;
}
public override string ToString()
{
return $"{Name} {Title}";
}
}
class Company
{
public int Id { get; set; }
public string Name { get; set; }
public virtual List<Phone> Phones { get; set; }
public override string ToString()
{
return Name;
}
}
class Context : DbContext
{
public DbSet<Company> Companies { get; set; }
public DbSet<Phone> Phones { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlite(@"Data Source=MobileDB.db;");
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Company>().ToTable("Companies");
modelBuilder.Entity<Phone>().ToTable("Phones");
}
}
Вот миграция:
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "Companies",
columns: table => new
{
Id = table.Column<int>(nullable: false)
.Annotation("Sqlite:Autoincrement", true),
Name = table.Column<string>(nullable: true)
},
constraints: table =>
{
table.PrimaryKey("PK_Companies", x => x.Id);
});
migrationBuilder.CreateTable(
name: "Phones",
columns: table => new
{
Id = table.Column<int>(nullable: false)
.Annotation("Sqlite:Autoincrement", true),
Name = table.Column<string>(nullable: true),
Title = table.Column<string>(nullable: true),
CompanyId = table.Column<int>(nullable: true)
},
constraints: table =>
{
table.PrimaryKey("PK_Phones", x => x.Id);
table.ForeignKey(
name: "FK_Phones_Companies_CompanyId",
column: x => x.CompanyId,
principalTable: "Companies",
principalColumn: "Id",
onDelete: ReferentialAction.Restrict);
});
migrationBuilder.CreateIndex(
name: "IX_Phones_CompanyId",
table: "Phones",
column: "CompanyId");
}
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "Phones");
migrationBuilder.DropTable(
name: "Companies");
}