DbContextOptionBuilder不包含UseSqlServer的定义-VS 2017 / VS for Mac

时间:2018-09-26 06:43:21

标签: c# sql-server visual-studio .net-core

昨天我写了一些代码,该代码连接到本地数据库(SQL Server),并将一个表的项计数写到VS 2017的控制台中(昨天最新更新)。

我想从我的IMac(Docker + Docker中的SQL Server)在.net核心中托管一个api,所以我将项目加载到了VS for Mac(昨天最新更新)中。

当我以调试模式运行程序时,没有断点,它将引发内部错误异常。当我在此处创建新的断点时:

class Choice extends Model
{
    /**
     * The questions that belong to the choice.
     */
    public function questions()
    {
        return $this->belongsToMany('App\Question');
    }
}

它说:

  

DbContextOptionBuilder不包含UseSqlServer的定义

在Windows上的VS 2017中,一切正常。

我发现了很多解决此错误的“方法”,但是没有用。 (Microsoft.EntityFrameWorkCore使用,引用等)。

  

我应该在IMac上使用VS Code而不是Mac上的VS 2017吗?

这是我完整的dBContext代码:

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
    if (!optionsBuilder.IsConfigured)
    {
        optionsBuilder.UseSqlServer("server=localhost;database=mra_ss_db;user id=sa;password=MYPASSWORD;");
    }
}

以及控制台应用程序中的小Program.cs:

using System;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.SqlServer;
using Microsoft.EntityFrameworkCore.Metadata;

namespace MRA.DataAccessLayer_Core.Models
{
public partial class mra_ss_dbContext : DbContext
{
    public mra_ss_dbContext()
    {
    }

    public mra_ss_dbContext(DbContextOptions<mra_ss_dbContext> options)
        : base(options)
    {
    }

    public virtual DbSet<Companies> Companies { get; set; }
    public virtual DbSet<Countries> Countries { get; set; }
    public virtual DbSet<Genres> Genres { get; set; }
    public virtual DbSet<Languages> Languages { get; set; }
    public virtual DbSet<Movies> Movies { get; set; }
    public virtual DbSet<MoviesCompanies> MoviesCompanies { get; set; }
    public virtual DbSet<MoviesCountries> MoviesCountries { get; set; }
    public virtual DbSet<MoviesGenres> MoviesGenres { get; set; }
    public virtual DbSet<MoviesLanguages> MoviesLanguages { get; set; }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        if (!optionsBuilder.IsConfigured)
        {
            optionsBuilder.UseSqlServer("server=localhost;database=mra_ss_db;user id=sa;password=MYPASSWORD;");
        }
    }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Companies>(entity =>
        {
            entity.HasKey(e => e.CompanyId);

            entity.Property(e => e.CompanyId)
                .HasColumnName("CompanyID")
                .ValueGeneratedNever();

            entity.Property(e => e.LogoPath)
                .IsRequired()
                .IsUnicode(false);

            entity.Property(e => e.Name)
                .IsRequired()
                .IsUnicode(false);

            entity.Property(e => e.OriginCountry)
                .IsRequired()
                .IsUnicode(false);
        });

        modelBuilder.Entity<Countries>(entity =>
        {
            entity.HasKey(e => e.Iso31661);

            entity.Property(e => e.Iso31661)
                .HasColumnName("Iso_3166_1")
                .HasMaxLength(50)
                .IsUnicode(false)
                .ValueGeneratedNever();

            entity.Property(e => e.Name)
                .IsRequired()
                .IsUnicode(false);
        });

        modelBuilder.Entity<Genres>(entity =>
        {
            entity.HasKey(e => e.GenreId);

            entity.Property(e => e.GenreId)
                .HasColumnName("GenreID")
                .ValueGeneratedNever();

            entity.Property(e => e.Name)
                .IsRequired()
                .IsUnicode(false);
        });

        modelBuilder.Entity<Languages>(entity =>
        {
            entity.HasKey(e => e.Iso6391);

            entity.Property(e => e.Iso6391)
                .HasColumnName("Iso_639_1")
                .HasMaxLength(50)
                .IsUnicode(false)
                .ValueGeneratedNever();

            entity.Property(e => e.Name)
                .IsRequired()
                .HasMaxLength(50)
                .IsUnicode(false);
        });

        modelBuilder.Entity<Movies>(entity =>
        {
            entity.HasKey(e => e.MovieId);

            entity.Property(e => e.MovieId)
                .HasColumnName("MovieID")
                .ValueGeneratedNever();

            entity.Property(e => e.BackdropPath).IsUnicode(false);

            entity.Property(e => e.ImdbId)
                .HasColumnName("ImdbID")
                .IsUnicode(false);

            entity.Property(e => e.OriginalLanguage)
                .IsRequired()
                .HasMaxLength(50)
                .IsUnicode(false);

            entity.Property(e => e.OriginalTitle)
                .IsRequired()
                .IsUnicode(false);

            entity.Property(e => e.Overview).IsUnicode(false);

            entity.Property(e => e.PosterPath).IsUnicode(false);

            entity.Property(e => e.ReleaseDate).HasColumnType("datetime");

            entity.Property(e => e.Status)
                .IsRequired()
                .HasMaxLength(50)
                .IsUnicode(false);

            entity.Property(e => e.Tagline).IsUnicode(false);

            entity.Property(e => e.Title)
                .IsRequired()
                .IsUnicode(false);
        });

        modelBuilder.Entity<MoviesCompanies>(entity =>
        {
            entity.Property(e => e.CompanyId).HasColumnName("CompanyID");

            entity.Property(e => e.MovieId).HasColumnName("MovieID");

            entity.HasOne(d => d.Company)
                .WithMany(p => p.MoviesCompanies)
                .HasForeignKey(d => d.CompanyId)
                .OnDelete(DeleteBehavior.ClientSetNull)
                .HasConstraintName("FK__MoviesCom__Compa__398D8EEE");

            entity.HasOne(d => d.Movie)
                .WithMany(p => p.MoviesCompanies)
                .HasForeignKey(d => d.MovieId)
                .OnDelete(DeleteBehavior.ClientSetNull)
                .HasConstraintName("FK__MoviesCom__Movie__3A81B327");
        });

        modelBuilder.Entity<MoviesCountries>(entity =>
        {
            entity.Property(e => e.Iso31661)
                .IsRequired()
                .HasColumnName("Iso_3166_1")
                .HasMaxLength(50)
                .IsUnicode(false);

            entity.Property(e => e.MovieId).HasColumnName("MovieID");

            entity.HasOne(d => d.Iso31661Navigation)
                .WithMany(p => p.MoviesCountries)
                .HasForeignKey(d => d.Iso31661)
                .OnDelete(DeleteBehavior.ClientSetNull)
                .HasConstraintName("FK__MoviesCou__Iso_3__3F466844");

            entity.HasOne(d => d.Movie)
                .WithMany(p => p.MoviesCountries)
                .HasForeignKey(d => d.MovieId)
                .OnDelete(DeleteBehavior.ClientSetNull)
                .HasConstraintName("FK__MoviesCou__Movie__403A8C7D");
        });

        modelBuilder.Entity<MoviesGenres>(entity =>
        {
            entity.Property(e => e.GenreId).HasColumnName("GenreID");

            entity.Property(e => e.MovieId).HasColumnName("MovieID");

            entity.HasOne(d => d.Genre)
                .WithMany(p => p.MoviesGenres)
                .HasForeignKey(d => d.GenreId)
                .OnDelete(DeleteBehavior.ClientSetNull)
                .HasConstraintName("FK__MoviesGen__Genre__3C69FB99");

            entity.HasOne(d => d.Movie)
                .WithMany(p => p.MoviesGenres)
                .HasForeignKey(d => d.MovieId)
                .OnDelete(DeleteBehavior.ClientSetNull)
                .HasConstraintName("FK__MoviesGen__Movie__3B75D760");
        });

        modelBuilder.Entity<MoviesLanguages>(entity =>
        {
            entity.Property(e => e.Iso6391)
                .IsRequired()
                .HasColumnName("Iso_639_1")
                .HasMaxLength(50)
                .IsUnicode(false);

            entity.Property(e => e.MovieId).HasColumnName("MovieID");

            entity.HasOne(d => d.Iso6391Navigation)
                .WithMany(p => p.MoviesLanguages)
                .HasForeignKey(d => d.Iso6391)
                .OnDelete(DeleteBehavior.ClientSetNull)
                .HasConstraintName("FK__MoviesLan__Iso_6__3E52440B");

            entity.HasOne(d => d.Movie)
                .WithMany(p => p.MoviesLanguages)
                .HasForeignKey(d => d.MovieId)
                .OnDelete(DeleteBehavior.ClientSetNull)
                .HasConstraintName("FK__MoviesLan__Movie__3D5E1FD2");
        });
    }
}
}

如果有人可以帮助我,请告诉我!

1 个答案:

答案 0 :(得分:0)

该问题的解决方案是添加软件包Microsoft.EntityFrameworkCore.SqlServer,然后在添加using引用后出现UseSqlServer。我希望它适用于与我遇到同样问题的任何人。