Sql server 更新数据库efcore asp.net core时出错 名称空间LoopSquad.Core.Entities.Addresses { 公共课堂演讲 { public int AddressId{get;set;} 公共字符串非名称{get;set;} 公共字符串地址L1{get;set;} 公共字符串地址L2{get;set;} 公共字符串{get;set;} 公共字符串country{get;set;} 公共字符串邮政编码{get;set;} [外国钥匙(“客户”)] public int CustomerId{get;set;} 公共客户。客户客户{get;set;} 公共ICollection作业{get;set;} } } 命名空间LoopSquad.Core.Entities.Customers { 公共类客户 { [关键] public int CustomerId{get;set;} 公共字符串CompanyName{get;set;} [外键(“FKCustomerType”)] public int CustomerTypeId{get;set;} 公共CustomerType CustomerType{get;set;} 公共ICollection地址{get;set;} } } 命名空间LoopSquad.Core.Entities.Jobs { 公开课工作 { [关键] public int JobId{get;set;} [外国客户]] public int CustomerId{get;set;} 公共客户。客户客户{get;set;} [外键(“FKAddress”)] public int AddressId{get;set;} 公共地址。地址{get;set;} 公共日期时间BookedDateTime{get;set;} 公共日期时间CreatedDateTime{get;set;} [外键(“FKUser”)] public int UserId{get;set;} public Users.ApplicationUser ApplicationUser{get;set;} [外键(“FRoomLayout”)] public int RoomLayoutId{get;set;} 公共RoomLayout RoomLayout{get;set;} [外键(“FKJobType”)] public int JobTypeId{get;set;} public JobType JobType{get;set;} [外键(“FKLoopType”)] public int loopTypeId{get;set;} 公共LoopType LoopType{get;set;} [外键(“FKJobStatus”)] public int JobStatusId{get;set;} public JobStatus JobStatus{get;set;} } }
我重现了您的问题,这是因为您默认启用了级联删除,这将导致您的关系循环 例如,尝试在dbcontext中使用Sql server 更新数据库efcore asp.net core时出错 名称空间LoopSquad.Core.Entities.Addresses { 公共课堂演讲 { public int AddressId{get;set;} 公共字符串非名称{get;set;} 公共字符串地址L1{get;set;} 公共字符串地址L2{get;set;} 公共字符串{get;set;} 公共字符串country{get;set;} 公共字符串邮政编码{get;set;} [外国钥匙(“客户”)] public int CustomerId{get;set;} 公共客户。客户客户{get;set;} 公共ICollection作业{get;set;} } } 命名空间LoopSquad.Core.Entities.Customers { 公共类客户 { [关键] public int CustomerId{get;set;} 公共字符串CompanyName{get;set;} [外键(“FKCustomerType”)] public int CustomerTypeId{get;set;} 公共CustomerType CustomerType{get;set;} 公共ICollection地址{get;set;} } } 命名空间LoopSquad.Core.Entities.Jobs { 公开课工作 { [关键] public int JobId{get;set;} [外国客户]] public int CustomerId{get;set;} 公共客户。客户客户{get;set;} [外键(“FKAddress”)] public int AddressId{get;set;} 公共地址。地址{get;set;} 公共日期时间BookedDateTime{get;set;} 公共日期时间CreatedDateTime{get;set;} [外键(“FKUser”)] public int UserId{get;set;} public Users.ApplicationUser ApplicationUser{get;set;} [外键(“FRoomLayout”)] public int RoomLayoutId{get;set;} 公共RoomLayout RoomLayout{get;set;} [外键(“FKJobType”)] public int JobTypeId{get;set;} public JobType JobType{get;set;} [外键(“FKLoopType”)] public int loopTypeId{get;set;} 公共LoopType LoopType{get;set;} [外键(“FKJobStatus”)] public int JobStatusId{get;set;} public JobStatus JobStatus{get;set;} } },sql-server,asp.net-core,entity-framework-core,Sql Server,Asp.net Core,Entity Framework Core,我重现了您的问题,这是因为您默认启用了级联删除,这将导致您的关系循环 例如,尝试在dbcontext中使用OnDelete(DeleteBehavior.Restrict)禁用它 namespace LoopSquad.Core.Entities.Addresses { public class Address { public int AddressId { get; set; } public string NoName { get; set;
OnDelete(DeleteBehavior.Restrict)
禁用它
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; }
}
}
公共类ApplicationDbContext:DbContext
{
公共应用程序上下文(DbContextOptions请解释您获取的代码格式是什么错误?在表“Jobs”上生成外键约束“FK_Jobs_Customers_CustomerId”可能会导致循环或多个级联路径。请在删除时指定“NO ACTION”,或在更新时指定“NO ACTION”,或修改其他外键约束。无法创建约束或索引。请参阅上文s errors.PM>未能执行DbCommand(24ms)[Parameters=[],CommandType='Text',CommandTimeout='30']创建表[Jobs]([JobId]int NOT NULL标识,[CustomerId]int NOT NULL,[AddressId]int NOT NULL,[BookedDateTime]datetime2 notnull,[CreatedDateTime]datetime2 notnull,[UserId]int NOT NULL,[applicationserid]nvarchar(450)NULL、[RoomLayoutId]int NOT NULL、[JobTypeId]int NOT NULL、[loopTypeId]int NOT NULL、,
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);
}
}