c#ef代码第一个外键没有工作

发布于 2025-01-21 04:13:40 字数 2728 浏览 0 评论 0原文

我的实体框架代码 - 第一个方法创建了以下两个表,该表应连接我的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必须具有一个复合主键,该键由objidcustomcolumnid组成。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文