【发布时间】:2018-12-19 06:14:03
【问题描述】:
为什么我最初的 update-database 失败了,我需要在我的数据库表类中进行哪些更改才能使其正常工作?
当然,我可以将迁移脚本中的onDelete: ReferentialAction.Cascade 更改为onDelete: ReferentialAction.NoAction,但是我的应用程序中会遇到其他问题。我正在寻求无需编辑由add-migration 生成的迁移脚本的解决方案。换句话说,我愿意对我的数据库架构进行更改。
我想要的行为是,当我删除Product 时,关联的ProductPropertyOptionForProducts 也被删除,但不是相反,而不是与ProductPropertyOptionForProducts 关联的ProductPropertyOption。
这是迁移输出错误消息:
在表“PropertyOptionsForProducts”上引入 FOREIGN KEY 约束“FK_PropertyOptionsForProducts_ProductPropertyOptions_ProductPropertyOptionId”可能会导致循环或多个级联路径。指定 ON DELETE NO ACTION 或 ON UPDATE NO ACTION,或修改其他 FOREIGN KEY 约束。 无法创建约束或索引。查看以前的错误。
生成的导致错误的 SQL 命令:
CREATE TABLE[PropertyOptionsForProducts] (
[Id] int NOT NULL IDENTITY,
[CustomNumberValue] decimal (18, 2) NOT NULL,
[CustomRangeFrom] decimal (18, 2) NOT NULL,
[CustomRangeTo] decimal (18, 2) NOT NULL,
[CustomStringValue] nvarchar(max) NULL,
[ProductId] int NOT NULL,
[ProductPropertyId] int NOT NULL,
[ProductPropertyOptionId] int NOT NULL,
CONSTRAINT[PK_PropertyOptionsForProducts] PRIMARY KEY([Id]),
CONSTRAINT[FK_PropertyOptionsForProducts_Products_ProductId]
FOREIGN KEY([ProductId])
REFERENCES[Products] ([Id]) ON DELETE CASCADE,
CONSTRAINT[FK_PropertyOptionsForProducts_ProductPropertyOptions_ProductPropertyOptionId]
FOREIGN KEY([ProductPropertyOptionId])
REFERENCES[ProductPropertyOptions] ([Id]) ON DELETE CASCADE
);
课程:
public class ProductPropertyOption
{
public int Id { get; set; }
public int ProductPropertyId { get; set; }
// some more properties
public ProductProperty Property { get; set; }
public ICollection<PropertyOptionForProduct> PropertyOptionForProducts { get; set; }
}
public class PropertyOptionForProduct
{
public int Id { get; set; }
public int ProductId { get; set; }
public int ProductPropertyId { get; set; }
public int ProductPropertyOptionId { get; set; }
// some more properties
public Product Product { get; set; }
public ProductPropertyOption ProductPropertyOption { get; set; }
}
public class Product
{
public int Id { get; set; }
public bool Published { get; set; }
public int ProductGroupId { get; set; }
public int ProductGroupSortOrder { get; set; }
// some more properties
public int ProductTypeId { get; set; }
public ICollection<ProductImage> Images { get; set; }
public ICollection<PropertyOptionForProduct> ProductPropertyOptionForProducts { get; set; }
public ICollection<IdentifierForProduct> IdentifierForProducts { get; set; }
public ProductType Type { get; set; }
public ICollection<FrontPageProduct> InFrontPages { get; set; }
public ICollection<ProductInCategory> InCategories { get; set; }
}
public class ProductType
{
public int Id { get; set; }
public string Title { get; set; }
public List<ProductIdentifierInType> Identifiers { get; set; }
public List<ProductProperty> Properties { get; set; }
public ICollection<Product> Products { get; set; }
}
public class ProductProperty
{
public int Id { get; set; }
public int ProductTypeId { get; set; }
// some more properties
public List<ProductPropertyOption> Options { get; set; }
public ProductType ProductType { get; set; }
}
图示的数据库(产品和类别部分):
【问题讨论】:
-
多个级联路径是从 ProductType 到 PropertyOptionForProduct:(1) ProductType -> Product -> PropertyOptionForProduct 和 (2) ProductType -> ProductProperty -> ProductPropertyOption -> PropertyOptionForProduct跨度>
-
您是否尝试过绘制各个类以及它们之间的引用?我觉得你有太多的关系无法理解:)
-
@IvanStoev 在我的
ProductType-view 中,我需要包含ProductProperty->ProductPropertyOption,在Product-view 中,我需要包含PropertyOptionForProduct-> @ 987654338@ ->ProductProperty。在没有多个级联路径的情况下如何做到这一点? -
@Boris 我有一个简单的地图,在方框之间用鱼尾纹表示关系。我已将其添加到问题的底部。 :)
标签: c# database-design entity-framework-core entity-framework-migrations ef-core-2.0