Entity Framework 中的空迁移填充(只有 Up() 和 Down() 两个空方法)

问题描述 投票:0回答:0

我正在使用 Entity Framework (EF7) 制作简单的数据库,我遇到了一个奇怪的问题。当我尝试更新我的数据库并使用创建迁移时 dotnet ef 迁移添加 ExampleMigration 迁移文件仅保留两种方法,根本没有主体。

The thing is everything is building and compiling as you can see

过程完成后,迁移文件如下所示:

using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace bazy_lab_entity.Migrations
{
    /// <inheritdoc />
    public partial class NotNullProductsList : Migration
    {
        /// <inheritdoc />
        protected override void Up(MigrationBuilder migrationBuilder)
        {

        }

        /// <inheritdoc />
        protected override void Down(MigrationBuilder migrationBuilder)
        {

        }
    }
}    

上下文文件:

using Microsoft.EntityFrameworkCore;
using System;

namespace bazy_lab_entity
{
    public class ProductContext : DbContext
    {
        public DbSet<Product> Products { get; set; }

        public DbSet<Supplier> Suppliers { get; set; }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            base.OnConfiguring(optionsBuilder);
            optionsBuilder.UseSqlite("Datasource=ProductsDatabase");
        }
    }
}

我的桌子是这样的:

namespace bazy_lab_entity
{
    public class Product
    {
        public int ProductID { get; set; }
        public string ProductName { get; set; }
        public int UnitsOnStock { get; set; }
    }
}`

`namespace bazy_lab_entity
{
    public class Supplier
    {
        public int SupplierId { get; set; }
        public string? CompanyName { get; set; }
        public string? Street { get; set; }
        public string? City { get; set; }
        public List<Product> ProductsList { get; set; }
    }
}

还有项目的设置:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net7.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.EntityFrameworkCore" Version="7.0.4" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="7.0.4">
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
      <PrivateAssets>all</PrivateAssets>
    </PackageReference>
    <PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="7.0.4" />
  </ItemGroup>

  <ItemGroup>
    <None Update="ProdcutDatabase">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </None>
    <None Update="ProductsDatabase">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </None>
  </ItemGroup>

  <ItemGroup>
    <Folder Include="Migrations\" />
  </ItemGroup>

</Project>

这是我已经尝试过的东西:

  1. 将虚拟添加到上下文文件
using Microsoft.EntityFrameworkCore;
using System;

namespace bazy_lab_entity
{
    public class ProductContext : DbContext
    {
        public DbSet<Product> Products { get; set; }

        public DbSet<Supplier> Suppliers { get; set; }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            base.OnConfiguring(optionsBuilder);
            optionsBuilder.UseSqlite("Datasource=ProductsDatabase");
        }
    }
}
  1. 删除上下文快照模型。
  2. 删除整个数据库并重新创建它。 我多次尝试了每种方法。

我现在真的不知道如何解决它。那时我感到绝望。

如果有人能帮助我,我将非常高兴和感激。

database entity-framework
© www.soinside.com 2019 - 2024. All rights reserved.