Код: Выделить всё
public abstract class Sector
{
public string ID {get; set;}
public string Name {get; set;}
public Sector(){}
}
Код: Выделить всё
public class GICSSector: Sector
{
public virtual ICollection IndustryGroups {get; set;}
}
Код: Выделить всё
public DbSet GICSSectors {get; set;}
Код: Выделить всё
public static void UpdateEntitiesFromCSV(MyContextFactory factory, string fileName) where T : class
{
var entities = new List();
// ... Load entities from the CSV
// ... Create the objects and add them to the list
// Add the objects to the database
using (var db = factory.Create(new DbContextFactoryOptions()))
{
var set = db.Set();
foreach(T e in entities)
{
set.Add(e);
}
db.SaveChanges();
}
}
Код: Выделить всё
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
//...
// GICSSector
modelBuilder.Entity().HasKey(s => new { s.ID });
modelBuilder.Entity().Property(s => s.ID).HasMaxLength(2);
modelBuilder.Entity().Property(s => s.Name).IsRequired();
modelBuilder.Entity().Property(s => s.Name).HasMaxLength(50);
}
Ошибка SQLite 1: 'нет такой таблицы: сектора'.
Если я проверю тип с помощью typeof(T) или используя myEntity.GetType(), я получаю тот же ожидаемый результат: MyNamespace.GICSSector
Почему EF Core хочет сохранить его в таблице под названием «Секторы» ( базовый тип), а не в ожидаемых GICSSectors?
Как это исправить?
Примечание: метод является универсальным и не будет использоваться только для обработки классов, наследуемых от Sector.
Подробнее здесь: https://stackoverflow.com/questions/424 ... rived-type
Мобильная версия