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

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

我正在使用 Entity Framework Core (EF Core 7) 创建一个简单的数据库,我遇到了一个奇怪的问题。

当我尝试更新我的数据库并使用创建迁移时

dotnet ef migrations add 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
1个回答
0
投票

添加关系配置将修复它。

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    modelBuilder.Entity<Supplier>()
        .HasMany(s => s.ProductsList)
        .WithOne(p => p.Supplier)
        .IsRequired();

    base.OnModelCreating(modelBuilder);
}
© www.soinside.com 2019 - 2024. All rights reserved.