LINQ到SQL的DataContext没有更新的外键关系 [英] Linq to SQL datacontext not updating for foreign key relationships

查看:137
本文介绍了LINQ到SQL的DataContext没有更新的外键关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在写对使用L2S一个仓库的数据库测试。在我的数据库中,我有一个清单实体和 AllocatedTransaction 实体。该AllocatedTransaction实体有一个外键清单的ID。该DDL看起来是这样的:

I'm writing a database test against a repository that uses L2S. In my database I have a Manifest entity and an AllocatedTransaction entity. The AllocatedTransaction entity has a foreign key to the Manifest's id. The DDL looks something like this:

Manifest:
Id - int - identity

AllocateTransaction:
Id - int - identity
Quantity - int
ManifestId - FK to Manifest

在我的测试,我检查,看看是否有AllocatedTransactions回来的清单。该测试是这样的:

In my test, I'm checking to see if AllocatedTransactions are coming back with the Manifest. The test looks like this:

[TestMethod]
public void FindByIdTest()
{
    using (data.EZTracDataContext dataContext = new data.EZTracDataContext())
    {
        using (new TransactionScope())
        {
            data.Manifest manifest = _testDataProvider.AddManifest(dataContext);

            data.AllocatedTransaction allocatedTransaction = _testDataProvider.AddEligibilityAllocated(dataContext, 5, manifest.Id);

            ManifestRepository repository = CreateRepository(dataContext);

            var actual = repository.FindById(manifest.Id).AllocatedTransactions;
            var expected = new[] { new domain.AllocatedTransaction(allocatedTransaction.Id, 5, manifest.Id) }.ToList();

            CollectionAssertAreEqual(actual, expected);
        }
    }
}



_testDataProvider 只是使用的DataContext 记录添加到数据库中。在 FindById 方法是这样的:

The _testDataProvider just adds records to the database using the passed in dataContext. The FindById method looks like this:

public domain.Manifest FindById(int id)
{
    var persistedManifest = GetPersistedManifest(id);
    var requestedManifest = GetDomainManifestFromData(persistedManifest);
    return requestedManifest;
}

private Manifest GetPersistedManifest(int manifestId)
{
    return (from manifests in DataContext.Manifests
            where manifests.Id == manifestId
            select manifests).FirstOrDefault();
}



我的问题是从DataContext的回来没有AllocateTransaction的清单实体与其关联。奇怪的是预先存在的舱单的回来附有他们的AllocatedTransactions。可以使用插入记录和检索的记录是导致此相同的DataContext对象?这是L2S一个bug?

My problem is the Manifest entity coming back from the DataContext does not have the AllocateTransaction associated to it. The strange thing is pre-existing Manifests do come back with their AllocatedTransactions attached. Could using the same DataContext object for inserting the records and retrieving the records be causing this? Is this a bug with L2S?

推荐答案

如果我理解你的问题,这是你想要做什么?

If I'm understanding your question, is this what you're trying to do?

using (new TransactionScope())
{
    Manifest manifest = new Manifest
    {
        AllocatedTransactions.Add(new AllocatedTransaction
        {
            Quantity = 5
        }
    };

    DataContext.Manifests.InsertOnSubmit(manifest);
    DataContext.SubmitChanges();

    Manifest newManifest = DataContext.Manifests.Where(a => a.ID == manifest.ID).SingleOrDefault(); 

    Assert.AreEqual(manifest.AllocatedTransactions.First().Quantity, newManifest.AllocatedTransactions.First().Quantity);
}

您也不需要手动检索与舱单相关的AllocatedTransaction实体,只要检索清单实体和我一样与 newManifest 对象和所有相关的 AllocatedTransaction 实体应跟随。

You also don't need to manually retrieve the AllocatedTransaction entities associated with the Manifest. Just retrieve the Manifest entity as I did with the newManifest object and all the associated AllocatedTransaction entities should follow along.

更新:

它看起来像你想在错误的方向连接。您需要附加AllocatedTransaction的清单,而不是其他的方式:

It looks like you're trying to attach in the wrong direction. You need to attach the AllocatedTransaction to the Manifest, not the other way around:

Manifest manifest = DataContext.Manifests.Single(a => a.ID == 27);
AllocatedTransaction allTrans = DataContext.AllocatedTransactions.Single(a => a.ID == 53);

manifest.AllocatedTransactions.Add(allTrans);
DataContext.SubmitChanges();

这假定两个舱单和AllocatedTransaction记录已经存在。我会强烈建议你*的的预填充 ManifestID 在AllocatedTransactions Field对象。如果添加它,因为我在上面已经证明,在LINQ引擎会自动解析和更新外键值。如果您设置的值时间提前,有可能甩开变更,并假设您要添加一个新的记录,而不是附加现有之一。

This assumes that both the Manifest and AllocatedTransaction records already exist. I would highly recommend that you *not prepopulate the ManifestID field in the AllocatedTransactions object. If you add it as I have demonstrated above, the LINQ engine will automatically resolve and update the foreign key value. If you set the value ahead of time, it may throw off the ChangeSet and assume that you are trying to add a new record rather than attach an existing one.

这篇关于LINQ到SQL的DataContext没有更新的外键关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆