我在尝试引用“CustomShipping”表中“StoreShippingMethod”表中的外键“Id”时遇到问题。当我尝试运行迁移时,会创建一个新的“StoreShippingMethodEntity”表,并且它不会引用运输模块中现有的“StoreShippingMethod”表。
这是我的 DbContext 文件:
using EntityFrameworkCore.Triggers;
using Microsoft.EntityFrameworkCore;
using VirtoCommerce.CatalogModule.Data.Model;
using VirtoCommerce.CustomShipping.Data.Models;
using VirtoCommerce.ShippingModule.Data.Model;
using VirtoCommerce.ShippingModule.Data.Repositories;
namespace VirtoCommerce.CustomShipping.Data.Repositories;
public class CustomShippingDbContext : DbContextWithTriggers
{
public CustomShippingDbContext(DbContextOptions<CustomShippingDbContext> options)
: base(options)
{
}
protected CustomShippingDbContext(DbContextOptions options)
: base(options)
{
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<CustomShippingEntity>().ToTable("CustomShipping").HasKey(x => x.Id);
modelBuilder.Entity<CustomShippingEntity>().Property(x => x.Id).HasMaxLength(128).ValueGeneratedOnAdd();
modelBuilder.Entity<CustomShippingEntity>().HasOne(x => x.StoreShippingMethod)
.WithMany().HasForeignKey(x => x.ShippingMethodId).IsRequired().OnDelete(DeleteBehavior.Restrict);
}
}
这是我的实体文件:
using System;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using VirtoCommerce.CatalogModule.Core.Model;
using VirtoCommerce.CatalogModule.Data.Model;
using VirtoCommerce.CustomShipping.Core.Models;
using VirtoCommerce.Platform.Core.Common;
using VirtoCommerce.Platform.Core.Domain;
using VirtoCommerce.ShippingModule.Core.Model;
using VirtoCommerce.ShippingModule.Data.Model;
namespace VirtoCommerce.CustomShipping.Data.Models
{
public class CustomShippingEntity : AuditableEntity, IDataEntity<CustomShippingEntity, CustomShippingRates>
{
[Required]
public string CategoryId { get; set; }
[Required]public string OptionName { get; set; }
public string OptionDescription { get; set; }
public decimal Rate { get; set; }
public decimal RateWithTax { get; set; }
public string Currency { get; set; }
public decimal DiscountAmount { get; set; }
public decimal DiscountAmountWithTax { get; set; }
[Required, StringLength(128)]
public string ShippingMethodId { get; set; }
public string ShippingRateId { get; set; }
public virtual StoreShippingMethodEntity StoreShippingMethod { get; set; }
public virtual CustomShippingRates ToModel(CustomShippingRates customShippingRates)
{
if(customShippingRates == null)
{
throw new ArgumentNullException(nameof(customShippingRates));
}
customShippingRates.Id = Id;
customShippingRates.CreatedBy = CreatedBy;
customShippingRates.CreatedDate = CreatedDate;
customShippingRates.ModifiedBy = ModifiedBy;
customShippingRates.ModifiedDate = ModifiedDate;
customShippingRates.CategoryId = CategoryId;
customShippingRates.ShippingRateId = ShippingRateId;
customShippingRates.OptionName = OptionName;
customShippingRates.OptionDescription = OptionDescription;
customShippingRates.Rate = Rate;
customShippingRates.RateWithTax = RateWithTax;
customShippingRates.Currency = Currency;
customShippingRates.DiscountAmount = DiscountAmount;
customShippingRates.DiscountAmountWithTax = DiscountAmountWithTax;
customShippingRates.ShippingMethodId = ShippingMethodId;
if (StoreShippingMethod!=null) {
customShippingRates.ShippingMethod = (ShippingMethod)StoreShippingMethod?.ToModel(AbstractTypeFactory<ShippingMethod>.TryCreateInstance());
}
return customShippingRates;
}
public virtual CustomShippingEntity FromModel(CustomShippingRates customShippingRates, PrimaryKeyResolvingMap pkMap)
{
if (customShippingRates == null)
{
throw new ArgumentNullException(nameof(customShippingRates));
}
pkMap.AddPair((IEntity)customShippingRates, this);
Id = customShippingRates.Id;
CreatedBy = customShippingRates.CreatedBy;
CreatedDate = customShippingRates.CreatedDate;
ModifiedBy = customShippingRates.ModifiedBy;
ModifiedDate = customShippingRates.ModifiedDate;
CategoryId = customShippingRates.CategoryId;
ShippingRateId = customShippingRates.ShippingRateId;
OptionName = customShippingRates.OptionName;
OptionDescription = customShippingRates.OptionDescription;
Rate = customShippingRates.Rate;
RateWithTax = customShippingRates.RateWithTax;
Currency = customShippingRates.Currency;
DiscountAmount = customShippingRates.DiscountAmount;
DiscountAmountWithTax = customShippingRates.DiscountAmountWithTax;
ShippingMethodId = customShippingRates.ShippingMethodId;
return this;
}
public void Patch(CustomShippingEntity target)
{
}
}
}
当我尝试运行迁移时,它会创建一个新表 StoreShippingMethodEntity,尽管它应该引用现有的 StoreShippingMethod 表。
这是我的迁移文件:-
using System;
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace VirtoCommerce.CustomShipping.Data.Migrations
{
public partial class Init : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "StoreShippingMethodEntity",
columns: table => new
{
Id = table.Column<string>(type: "nvarchar(450)", nullable: false),
Code = table.Column<string>(type: "nvarchar(128)", maxLength: 128, nullable: false),
Priority = table.Column<int>(type: "int", nullable: false),
LogoUrl = table.Column<string>(type: "nvarchar(2048)", maxLength: 2048, nullable: true),
TaxType = table.Column<string>(type: "nvarchar(64)", maxLength: 64, nullable: true),
IsActive = table.Column<bool>(type: "bit", nullable: false),
TypeName = table.Column<string>(type: "nvarchar(max)", nullable: true),
StoreId = table.Column<string>(type: "nvarchar(max)", nullable: true)
},
constraints: table =>
{
table.PrimaryKey("PK_StoreShippingMethodEntity", x => x.Id);
});
migrationBuilder.CreateTable(
name: "CustomShipping",
columns: table => new
{
Id = table.Column<string>(type: "nvarchar(128)", maxLength: 128, nullable: false),
CategoryId = table.Column<string>(type: "nvarchar(max)", nullable: false),
OptionName = table.Column<string>(type: "nvarchar(max)", nullable: false),
OptionDescription = table.Column<string>(type: "nvarchar(max)", nullable: true),
Rate = table.Column<decimal>(type: "decimal(18,2)", nullable: false),
RateWithTax = table.Column<decimal>(type: "decimal(18,2)", nullable: false),
Currency = table.Column<string>(type: "nvarchar(max)", nullable: true),
DiscountAmount = table.Column<decimal>(type: "decimal(18,2)", nullable: false),
DiscountAmountWithTax = table.Column<decimal>(type: "decimal(18,2)", nullable: false),
ShippingMethodId = table.Column<string>(type: "nvarchar(128)", maxLength: 128, nullable: false),
ShippingRateId = table.Column<string>(type: "nvarchar(max)", nullable: true),
CreatedDate = table.Column<DateTime>(type: "datetime2", nullable: false),
ModifiedDate = table.Column<DateTime>(type: "datetime2", nullable: true),
CreatedBy = table.Column<string>(type: "nvarchar(64)", maxLength: 64, nullable: true),
ModifiedBy = table.Column<string>(type: "nvarchar(64)", maxLength: 64, nullable: true)
},
constraints: table =>
{
table.PrimaryKey("PK_CustomShipping", x => x.Id);
table.ForeignKey(
name: "FK_CustomShipping_StoreShippingMethodEntity_ShippingMethodId",
column: x => x.ShippingMethodId,
principalTable: "StoreShippingMethodEntity",
principalColumn: "Id",
onDelete: ReferentialAction.Restrict);
});
migrationBuilder.CreateIndex(
name: "IX_CustomShipping_ShippingMethodId",
table: "CustomShipping",
column: "ShippingMethodId");
}
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "CustomShipping");
migrationBuilder.DropTable(
name: "StoreShippingMethodEntity");
}
}
}
我正在使用此命令来添加迁移:-
EntityFrameworkCore\Add-Migration Init -Context VirtoCommerce.CustomShipping.Data.Repositories.CustomShippingDbContext -Verbose -OutputDir 迁移 -Project VirtoCommerce.CustomShipping.Data -StartupProject VirtoCommerce.CustomShipping.Data -调试
您有两个实体:
CustomShippingEntity
和StoreShippingMethodEntity
,前者引用后者。
您已经为包含
CustomShippingEntity
类型实体的表(CustomShipping)指定了自定义名称,但没有为包含 StoreShippingMethodEntity
实体的表指定了自定义名称,因此使用约定建立的名称创建新表是正常的:StoreShippingMethodEntity。
另外,您提到已经有一个名为
StoreShippingMethod
的现有表?如果您使用 Code First 会怎样?