Reputation: 303
I'm just trying to use migrations in my .Net Core class library, but for some reason I have the following return:
$ dotnet ef migrations add InitialMigration
No executable found matching command "dotnet-ef"
I googled many times, but none of examples are applicable to my scenario.
My solution looks like this:
using Microsoft.EntityFrameworkCore;
using VirtualStore.Data.Mapping;
namespace VirtualStore.Data
{
public class Context<T> : DbContext where T : Entity
{
public DbSet<T> Entity { get; set; }
public Context()
{
Database.EnsureCreated();
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlServer("Data Source=DESKTOP-3UEM3PC;Initial Catalog=VIRTUALSTORE;Integrated Security=SSPI;");
base.OnConfiguring(optionsBuilder);
}
}
}
And my repository class:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using VirtualStore.Data.Mapping;
using VirtualStore.Models.Data.Repository;
namespace VirtualStore.Data
{
public class Repository<T> : IRepository<T> where T : Entity
{
public Context<T> Context { get; set; }
public void Delete(T entity)
{
using (Context = new Context<T>())
{
Context.Remove(entity);
Context.SaveChanges();
}
}
public void DeleteAll(IEnumerable<T> entities)
{
using (Context = new Context<T>())
{
Context.RemoveRange(entities);
Context.SaveChanges();
}
}
public IEnumerable<T> Get(Func<T, bool> predicate)
{
using (Context = new Context<T>())
{
return Context.Entity.Where(predicate).ToList();
}
}
public IEnumerable<T> GetAll()
{
using (Context = new Context<T>())
{
var all = Context.Entity.OrderBy(x => x.Id).ToList();
return all;
}
}
public T GetById(long id)
{
using (Context = new Context<T>())
{
return Context.Entity.Where(x => x.Id == id).OrderBy(x => x.Id).FirstOrDefault();
}
}
public void InsertAll(IEnumerable<T> entities)
{
using (Context = new Context<T>())
{
Context.Entity.AddRange(entities);
Context.SaveChanges();
}
}
public void Insert(T entity)
{
using (Context = new Context<T>())
{
Context.Entity.Add(entity);
Context.SaveChanges();
}
}
public void Update(T entity)
{
using (Context = new Context<T>())
{
Context.Update(entity);
Context.SaveChanges();
}
}
public void UpdateAll(IEnumerable<T> entities)
{
using (Context = new Context<T>())
{
Context.UpdateRange(entities);
Context.SaveChanges();
}
}
}
}
Project link: https://github.com/otaviolarrosa/VirtualStore
Can anyone help me? What is wrong with my command to start using migrations?
Upvotes: 3
Views: 568
Reputation: 25141
Add the following to the VirtualStore.Data.csproj file in your VirtualStore.Data project.
<ItemGroup>
<DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="2.0.1" />
</ItemGroup>
After saving the change, you should be able to access those commands from a command prompt. The command prompt current directory will need to be in the same directory as the VirtualStore.Data project.
Without this reference, those commands are not available.
Upvotes: 7