【发布时间】:2021-01-05 19:07:27
【问题描述】:
我正在使用带有实体框架的代码优先方法开发数据库。
我想在几个表之间使用外键,例如:
public class Producer
{
[Key]
public int Id { get; set; }
[Required]
[StringLength(254)]
public string Name { get; set; }
}
然后另一个文件模型被简化为
public class Product
{
[Key]
public int Id { get; set; }
[Required]
[StringLength(254)]
public string Name { get; set; }
}
我想要的是在 Product 上定义一个 ProducerId 字段并将其链接到 Producers ID 字段,但我不知道如何告诉它它链接到哪个模型/表。
谁能澄清一下?我认为根据我阅读的内容,我似乎需要使 ID 字段更具描述性,EF 会找到该字段,ProductId 是 Products Id 字段 - 但我仍然不肯定它会跨表工作。
这是正确的还是我错过了什么?
编辑:
我尝试了下面的第一个答案,但使用的表格不止两个,并且得到了这个错误:
fail: Microsoft.EntityFrameworkCore.Database.Command[20102]
Failed executing DbCommand (46ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
CREATE TABLE `Product` (
`ProductId` int NOT NULL AUTO_INCREMENT,
`ProducerId` int NOT NULL,
`ProductCategoryId` int NOT NULL,
`ProductStyleId` int NOT NULL,
`Name` varchar(254) NULL,
`Year` datetime NOT NULL,
`CreatedAt` datetime NOT NULL,
`CategoryId` int NOT NULL,
`StyleId` int NOT NULL,
`Image` varbinary(4000) NULL,
`TastingNotes` text NULL,
`Description` text NULL,
PRIMARY KEY (`ProductId`),
CONSTRAINT `FK_Product_Producers_ProducerId` FOREIGN KEY (`ProducerId`) REFERENCES `Producers` (`ProducerId`) ON DELETE CASCADE,
CONSTRAINT `FK_Product_ProductCategory_ProductCategoryId` FOREIGN KEY (`ProductCategoryId`) REFERENCES `ProductCategory` (`ProductCategoryId`) ON DELETE CASCADE,
CONSTRAINT `FK_Product_ProductStyle_ProductStyleId` FOREIGN KEY (`ProductStyleId`) REFERENCES `ProductStyle` (`ProductStyleId`) ON DELETE CASCADE
);
Failed executing DbCommand (46ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
CREATE TABLE `Product` (
`ProductId` int NOT NULL AUTO_INCREMENT,
`ProducerId` int NOT NULL,
`ProductCategoryId` int NOT NULL,
`ProductStyleId` int NOT NULL,
`Name` varchar(254) NULL,
`Year` datetime NOT NULL,
`CreatedAt` datetime NOT NULL,
`CategoryId` int NOT NULL,
`StyleId` int NOT NULL,
`Image` varbinary(4000) NULL,
`TastingNotes` text NULL,
`Description` text NULL,
PRIMARY KEY (`ProductId`),
CONSTRAINT `FK_Product_Producers_ProducerId` FOREIGN KEY (`ProducerId`) REFERENCES `Producers` (`ProducerId`) ON DELETE CASCADE,
CONSTRAINT `FK_Product_ProductCategory_ProductCategoryId` FOREIGN KEY (`ProductCategoryId`) REFERENCES `ProductCategory` (`ProductCategoryId`) ON DELETE CASCADE,
CONSTRAINT `FK_Product_ProductStyle_ProductStyleId` FOREIGN KEY (`ProductStyleId`) REFERENCES `ProductStyle` (`ProductStyleId`) ON DELETE CASCADE
);
MySql.Data.MySqlClient.MySqlException (0x80004005): Cannot add foreign key constraint
at MySql.Data.MySqlClient.MySqlStream.ReadPacket()
at MySql.Data.MySqlClient.NativeDriver.GetResult(Int32& affectedRow, Int64& insertedId)
at MySql.Data.MySqlClient.Driver.GetResult(Int32 statementId, Int32& affectedRows, Int64& insertedId)
at MySql.Data.MySqlClient.Driver.NextResult(Int32 statementId, Boolean force)
at MySql.Data.MySqlClient.MySqlDataReader.NextResult()
at MySql.Data.MySqlClient.MySqlCommand.ExecuteReader(CommandBehavior behavior)
at MySql.Data.MySqlClient.MySqlCommand.ExecuteReader()
at MySql.Data.MySqlClient.MySqlCommand.ExecuteNonQuery()
at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteNonQuery(RelationalCommandParameterObject parameterObject)
at Microsoft.EntityFrameworkCore.Migrations.MigrationCommand.ExecuteNonQuery(IRelationalConnection connection, IReadOnlyDictionary`2 parameterValues)
at Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationCommandExecutor.ExecuteNonQuery(IEnumerable`1 migrationCommands, IRelationalConnection connection)
at Microsoft.EntityFrameworkCore.Migrations.Internal.Migrator.Migrate(String targetMigration)
at Microsoft.EntityFrameworkCore.Design.Internal.MigrationsOperations.UpdateDatabase(String targetMigration, String contextType)
at Microsoft.EntityFrameworkCore.Design.OperationExecutor.UpdateDatabaseImpl(String targetMigration, String contextType)
at Microsoft.EntityFrameworkCore.Design.OperationExecutor.UpdateDatabase.<>c__DisplayClass0_0.<.ctor>b__0()
at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action)
C
【问题讨论】:
-
在
Product类上添加public int PRoductId {get;set;}然后添加public virtual Product Product { get; set; } -
[ForeignKey("PrdouctId")]是可选属性 -
我不认为我遵循你所说的......你能详细说明一下吗?你要我把id字段改成ProductId,然后加一个虚拟构造函数?我不明白你的第二条评论是什么意思
-
希望我的回答很清楚
-
另外我想说的是
ProducerId不是ProductId所以我的错。
标签: c# asp.net entity-framework ef-code-first