Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用实体框架时出现数据库第一个错误_C#_Entity Framework - Fatal编程技术网

C# 使用实体框架时出现数据库第一个错误

C# 使用实体框架时出现数据库第一个错误,c#,entity-framework,C#,Entity Framework,我首先使用.NETCore3.1数据库创建了一个WebAPI。在运行 Scaffold-DbContext "Server=192.168.100.100;Database=CHEID;User Id=sa; Password=mypassword;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models 它给了我一个称为“CHEIDContext”的DBcontext。我被告知需要将连接字符串放入appsett

我首先使用.NETCore3.1数据库创建了一个WebAPI。在运行

Scaffold-DbContext "Server=192.168.100.100;Database=CHEID;User Id=sa; Password=mypassword;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models
它给了我一个称为“CHEIDContext”的DBcontext。我被告知需要将连接字符串放入appsettings.json中,所以我就是这么做的

{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft": "Warning",
      "Microsoft.Hosting.Lifetime": "Information"
    }
  },
    "ConnectionStrings": {
      "DefaultConnectionString": "Server=192.168.100.100;Database=CHEID;User Id=sa; Password=mypassword;"
    },
  "AllowedHosts": "*"
}
这是我的CHEIDContext.cs

public partial class CHEIDContext : DbContext
    {
        public CHEIDContext()
        {
        }

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

        public virtual DbSet<EntryImages> EntryImages { get; set; }
        public virtual DbSet<EntryInfo> EntryInfo { get; set; }
        public virtual DbSet<StagingDataCash> StagingDataCash { get; set; }
        public virtual DbSet<StagingDataEtc> StagingDataEtc { get; set; }
        public virtual DbSet<StagingDataImage> StagingDataImage { get; set; }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            if (!optionsBuilder.IsConfigured)
            {
                IConfigurationRoot configuration = new ConfigurationBuilder()
                    .SetBasePath(Directory.GetCurrentDirectory())
                    .AddJsonFile("appsettings.json")
                    .Build();
                var connectionString = configuration.GetConnectionString("ConnectionStrings");
                optionsBuilder.UseSqlServer(connectionString);
            }
        }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<EntryImages>(entity =>
            {
                entity.HasKey(e => new { e.PlazaAutoId, e.PlazaCode })
                    .HasName("PK_TestImage");

                entity.Property(e => e.UploadDtime)
                    .HasColumnType("datetime")
                    .HasDefaultValueSql("(getdate())");
            });

            modelBuilder.Entity<EntryInfo>(entity =>
            {
                entity.HasKey(e => new { e.PlazaAutoId, e.PlazaCode });

                entity.Property(e => e.Action)
                    .HasMaxLength(1)
                    .IsUnicode(false);

                entity.Property(e => e.Id).ValueGeneratedOnAdd();

                entity.Property(e => e.PlateNumber)
                    .HasMaxLength(10)
                    .IsUnicode(false);

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

                entity.Property(e => e.UploadDtime)
                    .HasColumnType("datetime")
                    .HasDefaultValueSql("(getdate())");
            });

            modelBuilder.Entity<StagingDataCash>(entity =>
            {
                entity.Property(e => e.DetectionDateTime).HasColumnType("datetime");

                entity.Property(e => e.Epc)
                    .HasMaxLength(100)
                    .IsUnicode(false);

                entity.Property(e => e.ImageFileName)
                    .HasMaxLength(250)
                    .IsUnicode(false);

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

                entity.Property(e => e.PlateNumber)
                    .HasMaxLength(10)
                    .IsUnicode(false);

                entity.Property(e => e.PostingDtime)
                    .HasColumnType("datetime")
                    .HasDefaultValueSql("(getdate())");

                entity.Property(e => e.TagNumber)
                    .HasMaxLength(10)
                    .IsUnicode(false);

                entity.Property(e => e.Tid)
                    .HasMaxLength(100)
                    .IsUnicode(false);

                entity.Property(e => e.TxtFileName)
                    .HasMaxLength(250)
                    .IsUnicode(false);

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

            modelBuilder.Entity<StagingDataEtc>(entity =>
            {
                entity.Property(e => e.DetectionDateTime).HasColumnType("datetime");

                entity.Property(e => e.Epc)
                    .HasMaxLength(100)
                    .IsUnicode(false);

                entity.Property(e => e.ImageFileName)
                    .HasMaxLength(250)
                    .IsUnicode(false);

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

                entity.Property(e => e.PlateNumber)
                    .HasMaxLength(10)
                    .IsUnicode(false);

                entity.Property(e => e.PostingDtime)
                    .HasColumnType("datetime")
                    .HasDefaultValueSql("(getdate())");

                entity.Property(e => e.TagNumber)
                    .HasMaxLength(10)
                    .IsUnicode(false);

                entity.Property(e => e.Tid)
                    .HasMaxLength(100)
                    .IsUnicode(false);

                entity.Property(e => e.TxtFileName)
                    .HasMaxLength(250)
                    .IsUnicode(false);
            });

            modelBuilder.Entity<StagingDataImage>(entity =>
            {
                entity.HasKey(e => new { e.PlazaAutoId, e.PlazaCode, e.ImgSource });

                entity.Property(e => e.PostingDtime)
                    .HasColumnType("datetime")
                    .HasDefaultValueSql("(getdate())");

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

            OnModelCreatingPartial(modelBuilder);
        }

        partial void OnModelCreatingPartial(ModelBuilder modelBuilder);
    }
}
当我尝试使用“MVC Controller with views,using Entity Framework”创建控制器并使用数据上下文类CHEIDContext时,出现以下错误:

There was an error running the selected code generator: 'Value cannot be null, [Parameter:'connectionString']
而不是

var connectionString = configuration.GetConnectionString("ConnectionStrings");
使用

在appsettings.json文件中:

"ConnectionStrings": {
      "DefaultConnectionString": "Data Source=192.168.100.100;Initial Catalog=CHEID;User ID=sa; Password=mypassword;"
    }
而不是

var connectionString = configuration.GetConnectionString("ConnectionStrings");
使用

在appsettings.json文件中:

"ConnectionStrings": {
      "DefaultConnectionString": "Data Source=192.168.100.100;Initial Catalog=CHEID;User ID=sa; Password=mypassword;"
    }

您的配置是否已正确连接到其他内容?显示您的启动绑定(您在其中执行DI设置、配置、配置服务等),其中您提到了addDbContext/useSqlServeretc@CaiusJard请参阅我的编辑SIR,您的配置是否已正确连接其他内容?显示您的启动绑定(您在其中执行DI设置、配置、配置服务等),其中您提到了addDbContext/useSqlServeretc@CaiusJard请看我的编辑,先生