Добрый день!
Пытаюсь построить таблицы с помощью EF через Code First.
Не получается. Что-то я не так делаю.
Вот код инициализации.
Database.SetInitializer(new DropCreateDatabaseIfModelChanges<LCCheckpointContext>());
var db = new LCCheckpointContext(() => { return "LCCheckpointModel";});
db.Database.Initialize(true);
var req = new LCRequest { IsFreight = true };
db.Request.Add(req);
db.SaveChanges();
var query = from b in db.Request
orderby b.Id
select b;
foreach (var item in query)
{
Console.WriteLine(item.Id);
}
DBContext
public class LCCheckpointContext : DbContext
{
public LCCheckpointContext()
{
}
public LCCheckpointContext(Func<string> nameOrConnectionString)
: base(nameOrConnectionString())
{
Configuration.LazyLoadingEnabled = true;
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<LCRequest>();
modelBuilder.Configurations.Add(new RequestConfiguration());
}
public IDbSet<LCRequest> Request { get; set; }
}
Один класс модели
public class LCRequest
{
public LCRequest()
{
}
[Key]
public virtual int Id { get; set; }
public virtual bool IsFreight { get; set; }
public virtual string FreightDescr { get; set; }
public virtual Guid Barcode { get; set; }
}
Не создаётся база и в ней таблицы и всё тут.
Вот эксепшен
{"Migrations is enabled for context 'LCCheckpointContext' but the database does not exist or contains no mapped tables. Use Migrations to create the database and its tables, for example by running the 'Update-Database' command from the Package Manager Console."}
Собственно, что не так-то? Я уже пол дня мучаюсь, не могу понять что ему надо...