【问题标题】:Error on update-database efcore asp.net core更新数据库 efcore asp.net 核心上的错误
【发布时间】:2020-04-14 20:19:37
【问题描述】:
namespace LoopSquad.Core.Entities.Addresses
{
    public class Address
    {
        public int AddressId { get; set; }
        public string NoName { get; set; }
        public string AddressL1 { get; set; }
        public string AddressL2 { get; set; }
        public string Town { get; set; }
        public string County { get; set; }
        public string Postcode { get; set; }
        [ForeignKey("Customer")]
        public int CustomerId { get; set; }
        public Customers.Customer Customer { get; set; }

        public ICollection<Jobs.Job> Jobs { get; set; }
    }
}


namespace LoopSquad.Core.Entities.Customers
{
    public class Customer
    {
        [Key]
        public int CustomerId { get; set; }
        public string CompanyName { get; set; }
        [ForeignKey("FKCustomerType")]
        public int CustomerTypeId { get; set; }
        public CustomerType CustomerType { get; set; }

        public ICollection<Addresses.Address> Addresses { get; set; }

    }

}

namespace LoopSquad.Core.Entities.Jobs
{
    public class Job
    {
        [Key]
        public int JobId { get; set; }

        [ForeignKey("FKCustomer")]
        public int CustomerId { get; set; }
        public Customers.Customer Customer { get; set; }

        [ForeignKey("FKAddress")]
        public int AddressId { get; set; }
        public Addresses.Address Address { get; set; }


        public DateTime BookedDateTime { get; set; }
        public DateTime CreatedDateTime { get; set; }

        [ForeignKey("FKUser")]
        public int UserId { get; set; }
        public Users.ApplicationUser ApplicationUser { get; set; }
        [ForeignKey("FRoomLayout")]
        public int RoomLayoutId { get; set; }
        public RoomLayout RoomLayout { get; set; }

        [ForeignKey("FKJobType")]
        public int JobTypeId { get; set; }
        public JobType JobType { get; set; }

        [ForeignKey("FKLoopType")]
        public int loopTypeId { get; set; }
        public LoopType LoopType { get; set; }

        [ForeignKey("FKJobStatus")]
        public int JobStatusId { get; set; }
        public JobStatus JobStatus { get; set; }
    }
}

【问题讨论】:

  • 请解释您遇到了什么错误
  • 代码是诚实的
  • 在表 'Jobs' 上引入 FOREIGN KEY 约束 'FK_Jobs_Customers_CustomerId' 可能会导致循环或多个级联路径。指定 ON DELETE NO ACTION 或 ON UPDATE NO ACTION,或修改其他 FOREIGN KEY 约束。无法创建约束或索引。请参阅以前的错误。下午>
  • 执行 DbCommand 失败 (24ms) [Parameters=[], CommandType='Text', CommandTimeout='30'] CREATE TABLE [Jobs] ([JobId] int NOT NULL IDENTITY, [CustomerId] int NOT NULL,[AddressId] int NOT NULL,[BookedDateTime] datetime2 NOT NULL,[CreatedDateTime] datetime2 NOT NULL,[UserId] int NOT NULL,
  • [ApplicationUserId] nvarchar(450) NULL,[RoomLayoutId] int NOT NULL,[JobTypeId] int NOT NULL,[loopTypeId] int NOT NULL,

标签: sql-server asp.net-core entity-framework-core


【解决方案1】:

我重现了您的问题,这是因为您默认启用级联删除,这将导致您的关系出现循环。

尝试在 dbcontext 中使用OnDelete(DeleteBehavior.Restrict) 禁用它,例如

public class ApplicationDbContext : DbContext
{
    public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
        : base(options)
    {
    }      

    public DbSet<Address> Addresses { get; set; }
    public DbSet<Customer> Customers { get; set; }
    public DbSet<Job> Jobs { get; set; }
    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);
        modelBuilder.Entity<Job>().HasOne(p => p.Customer)
                                  .WithMany()
                                  .HasForeignKey(p => p.CustomerId)
                                  .OnDelete(DeleteBehavior.Restrict);
    }
}

参考https://docs.microsoft.com/en-us/ef/core/saving/cascade-delete

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-04-22
    • 2020-06-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多