簡體   English   中英

使用遷移與實體框架核心

[英]Using Migrations with Entity Framework Core

我只是想在我的.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

誰能幫我? 我的命令開始使用遷移有什么問題?

將以下內容添加到VirtualStore.Data項目中的VirtualStore.Data.csproj文件中。

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

保存更改后,您應該能夠從命令提示符訪問這些命令。 命令提示符當前目錄需要與VirtualStore.Data項目位於同一目錄中。

如果沒有此引用,則這些命令不可用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM