c#ef代码第一个外键没有工作
我的实体框架代码 - 第一个方法创建了以下两个表,该表应连接我的A导航属性:
-- TABLE CustomColumnData
CREATE TABLE [dbo].[CustomColumnData]
(
[ObjID] [char](42) NOT NULL,
[CustomColumnID] [tinyint] NOT NULL,
[Value] [nvarchar](max) NOT NULL,
CONSTRAINT [PK_CustomColumnData]
PRIMARY KEY CLUSTERED ([ObjID] ASC, [CustomColumnID] ASC)
WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF,
IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON,
ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
-- TABLE PickLineMapper
CREATE TABLE [dbo].[PickLineMapper]
(
[LeadingObjID] [char](42) NOT NULL,
[PickingListNumber] [int] NOT NULL,
CONSTRAINT [PK_PickLineMapper]
PRIMARY KEY CLUSTERED ([LeadingObjID] ASC)
WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF,
IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON,
ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
-- FOREIGN KEY
ALTER TABLE [CustomColumnData]
ADD CONSTRAINT [FK_CustomColumnData_PickLineMapper_ObjID]
FOREIGN KEY ([ObjID]) REFERENCES [PickLineMapper] ([LeadingObjID])
ON DELETE CASCADE;
此代码在错误代码中失败:
错误547 |
中。
Alter表语句与外键约束“ FK_CUSTOMCOLUMNDATA_PICKLINEMAPPER_OBJID”冲突。冲突发生在数据库“ MPA”,表“ DBO.Picklinemapper”,列'LeadingObjid'。
创建外键约束的代码如下:
private void InitializeForeignKeys(ModelBuilder modelBuilder)
{
modelBuilder.Entity<PickLineMapper>()
.HasMany(plm => plm.CustomColumnData)
.WithOne(ccd => ccd.PickLineMapper)
.HasForeignKey(ccd => ccd.ObjID);
}
这是两个类之一。从那里,我希望能够从customcolumndata
表访问所有元素,其中picklinemapper.leadingobjid
等于code> codiccolumndata.objid
,无论如何customcolumndata.customcolumnid
是什么。您必须知道,customcolumndata
必须具有一个复合主键,该键由objid
和customcolumnid
组成。
public class PickLineMapper
{
[Key]
[Column(Order = 1, TypeName = "char(42)")]
public string LeadingObjID { get; set; }
[Required]
[Column(Order = 2)]
public int PickingListNumber { get; set; }
public ICollection<CustomColumnData> CustomColumnData { get; set; }
public bool IsCommissioned => Convert.ToBoolean(
this.CustomColumnData
.Where(ccd => ccd.CustomColumnID == MpaContext.CustomFieldID_Commissioned)
.SingleOrDefault());
}
如何在这两个表之间正确定义工作导航属性?
My Entity Framework code-first approach creates the following two tables which should be connected my a navigation property:
-- TABLE CustomColumnData
CREATE TABLE [dbo].[CustomColumnData]
(
[ObjID] [char](42) NOT NULL,
[CustomColumnID] [tinyint] NOT NULL,
[Value] [nvarchar](max) NOT NULL,
CONSTRAINT [PK_CustomColumnData]
PRIMARY KEY CLUSTERED ([ObjID] ASC, [CustomColumnID] ASC)
WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF,
IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON,
ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
-- TABLE PickLineMapper
CREATE TABLE [dbo].[PickLineMapper]
(
[LeadingObjID] [char](42) NOT NULL,
[PickingListNumber] [int] NOT NULL,
CONSTRAINT [PK_PickLineMapper]
PRIMARY KEY CLUSTERED ([LeadingObjID] ASC)
WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF,
IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON,
ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
-- FOREIGN KEY
ALTER TABLE [CustomColumnData]
ADD CONSTRAINT [FK_CustomColumnData_PickLineMapper_ObjID]
FOREIGN KEY ([ObjID]) REFERENCES [PickLineMapper] ([LeadingObjID])
ON DELETE CASCADE;
This code fails with an error code:
Error 547 |
The ALTER TABLE statement conflicted with the FOREIGN KEY constraint "FK_CustomColumnData_PickLineMapper_ObjID". The conflict occurred in database "MPA", table "dbo.PickLineMapper", column 'LeadingObjID'.
The code for creating the foreign key constraint is the following:
private void InitializeForeignKeys(ModelBuilder modelBuilder)
{
modelBuilder.Entity<PickLineMapper>()
.HasMany(plm => plm.CustomColumnData)
.WithOne(ccd => ccd.PickLineMapper)
.HasForeignKey(ccd => ccd.ObjID);
}
Here's one of the two classes. From there, I want to be able to access all elements from the CustomColumnData
table where PickLineMapper.LeadingObjID
is equal to CustomColumnData.ObjID
, no matter what the CustomColumnData.CustomColumnID
is. You have to know, that CustomColumnData
must have a compound primary key consisting of ObjID
and CustomColumnID
.
public class PickLineMapper
{
[Key]
[Column(Order = 1, TypeName = "char(42)")]
public string LeadingObjID { get; set; }
[Required]
[Column(Order = 2)]
public int PickingListNumber { get; set; }
public ICollection<CustomColumnData> CustomColumnData { get; set; }
public bool IsCommissioned => Convert.ToBoolean(
this.CustomColumnData
.Where(ccd => ccd.CustomColumnID == MpaContext.CustomFieldID_Commissioned)
.SingleOrDefault());
}
How can I correctly define a working navigation property between those two tables?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论