使用 SQL Server 上的插入触发器生成的具有主主键的 Linq 主详细信息

发布于 2024-12-09 21:00:46 字数 1123 浏览 1 评论 0原文

我有一个场景,主表中的主键不是标识列。它是在 MS SQL Server 数据库中的“Instead of Insert”触发器上生成的。主键以 XYZ-11-001 的 varchar 格式生成。我正在尝试在主表上执行插入操作,然后使用 Linq 添加详细记录。我不断收到错误“无法将 NULL 值插入列“ParentID”、表“MyDB.dbo.Child”;列不允许为空。插入失败。”我的主键/外键关系设置正确。

我的简化代码如下:

using (DBDataContext context = new DBDataContext())
{
    ParentTable parent = new ParentTable();
    parent.FirstName = "Joe";
    parent.LastName = "Blow";

    master.Childs = new System.Data.Linq.EntitySet<child>();
    int index = 0;

    foreach (childObj in ChildrenObj) 
    {
        Index++;
        Child child = new Child();
        parent.Childs.Add(child);
        child.ID = index;  //Foreignkey + ID are Primarykey of the child table 
        child.FirstName = childObj.FirstName;
        child.LastName = childObj.LastName;


    }

    context.parents.InsertOnSubmit(parent);
    context.SubmitChanges();
}

我尝试将 child.Parent_ID 设置为 Parent.ID 值,但在“代替插入”触发器触发后它似乎没有更新(即使我在添加之前执行了 InsertOnSubmit孩子反对)。我尝试将其设置为任意值,但它会抱怨,因为它不会更新 child.NonIdendityColumnforeignKey 值。我想做的事情可能吗?在数据库中使用非生成的 ID 值作为主键似乎非常简单。

任何帮助将不胜感激。

提前致谢!

I have a scenario where my primary key in my master table is not an identity column. It is generated on a "Instead of Insert" trigger within an MS SQL Server database. The primary key is generated in a varchar format of XYZ-11-001. I am trying to do an insert on the master table then add detail records using Linq. I keep getting an error of "Cannot insert the value NULL into column 'ParentID', table 'MyDB.dbo.Child'; column does not allow nulls. INSERT fails." I do have my Primarykey/ForeignKey relationship setup correctly.

My simplified code is as follows:

using (DBDataContext context = new DBDataContext())
{
    ParentTable parent = new ParentTable();
    parent.FirstName = "Joe";
    parent.LastName = "Blow";

    master.Childs = new System.Data.Linq.EntitySet<child>();
    int index = 0;

    foreach (childObj in ChildrenObj) 
    {
        Index++;
        Child child = new Child();
        parent.Childs.Add(child);
        child.ID = index;  //Foreignkey + ID are Primarykey of the child table 
        child.FirstName = childObj.FirstName;
        child.LastName = childObj.LastName;


    }

    context.parents.InsertOnSubmit(parent);
    context.SubmitChanges();
}

I've tried setting the child.Parent_ID to the parent.ID value, but it doesn't seem to get updated after the "Instead of Insert" trigger fires (even if I do a InsertOnSubmit before adding the child objects). I've tried setting it to an arbitrary value, but it complains because it doesn't update the child.NonIdendityColumn ForeignKey value. Is what I am trying to do even possible? It seems quite trivial to use non generated ID values for a primary key within a database.

Any help would be much appreciated.

Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

極樂鬼 2024-12-16 21:00:46

问:“我想做的事情可能吗?”

答:是的,在数据​​库级别...

您可以像我一样使用 OUTPUT 子句 此处描述,但我不知道如何通过 LINQ 实现它。

Q: "Is what I am trying to do even possible?"

A: Yes, at the DB level...

You'd use the OUTPUT clause as I described here, but how you would implement that through LINQ I don't know.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文