【问题标题】:Using Migrations with Entity Framework Core将迁移与 Entity Framework Core 一起使用
【发布时间】:2017-11-29 13:39:29
【问题描述】:

我只是想在我的 .Net Core 类库中使用迁移,但由于某种原因,我得到了以下回报:

$ dotnet ef migrations add InitialMigration
No executable found matching command "dotnet-ef"

我google了很多次,但没有一个例子适用于我的场景。

我的解决方案如下所示:

还有我的上下文类:

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);
        }
    }
}

还有我的存储库类:

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();
            }
        }
    }
}

项目链接:https://github.com/otaviolarrosa/VirtualStore

谁能帮助我?我的命令开始使用迁移有什么问题?

【问题讨论】:

    标签: c# .net-core entity-framework-core


    【解决方案1】:

    将以下内容添加到您的 VirtualStore.Data 项目中的 VirtualStore.Data.csproj 文件中。

    <ItemGroup>
      <DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="2.0.1" />
    </ItemGroup>
    

    保存更改后,您应该能够从命令提示符访问这些命令。命令提示符当前目录需要与 VirtualStore.Data 项目位于同一目录中。

    如果没有这个引用,这些命令将不可用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-08-05
      • 2017-10-27
      • 2018-06-08
      • 2018-11-05
      • 2020-03-09
      • 2017-02-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多