gpt4 book ai didi

.net - 使用嵌套 POCO 进行 LINQ To SQL 操作(插入/更新)所需的帮助

转载 作者:行者123 更新时间:2023-12-01 19:00:58 33 4
gpt4 key购买 nike

好吧,我一直在尝试将我的模型转换为使用 LINQ,但不想丢弃当前分散在域中的 DTO 及其接口(interface)。

我设法找到这篇博文,它很好地概述了该过程:

Achieving POCOs in LINQ To SQL

我已经成功地将记录检索到正常工作的对象,但是,由于我的模型的嵌套性质,我似乎无法为子对象添加其他工作。也就是说,如果我创建一个子对象,并将引用设置为所需的父对象,LINQ to SQL 仍会引发异常,指出子对象对父对象的引用为 null。如果我尝试添加一个普通的旧父对象,它会成功,但直接添加子对象会失败

这是我失败的测试:

    [Test]
public void AddSelectionShouldAddSelectionToMarket()
{
Market market = (Market) new Repository().GetMarket(1);

Selection selection = new Selection();
selection.Market = market;

new Repository().AddSelection(selection);

Assert.IsTrue(selection.SID > 0);
}

这是错误消息:

System.InvalidOperationException: 尝试删除市场和选择之间的关系。但是,关系的外键之一 (Selection.MID) 不能设置为 null。

两个对象的相关部分:

[DataContract]
public class Selection : ISelection
{
private int mID;
[DataMember]
public int MID
{
get { return this.mID; }
set { this.mID = value; }
}

private Market market;
[DataMember]
public Market Market
{
get { return this.market; }
set
{
this.market = value;
this.mID = value.MID;
}
}
}

[DataContract]
public class Market : IMarket
{
private int mID;
[DataMember]
public int MID
{
get { return this.mID; }
protected set { this.mID = value; }
}

private List<Selection> selections;
[DataMember]
public List<Selection> Selections
{
get { return this.selections; }
set
{
this.selections = value;
// For LINQ
foreach (Selection selection in selections)
{
selection.MID = mID;
selection.Market = this;
}
}
}
}

我的 DA 代码:

        MarketsDataContext context = new MarketsDataContext();

DataLoadOptions options = new DataLoadOptions();
options.LoadWith<Selection>(s => s.Prices);
options.LoadWith<Market>(m => m.Selections);

context.LoadOptions = options;
return context;

和;

    public void AddSelection(ISelection selection)
{
using (MarketsDataContext context = MarketsDataContext.GetContext())
{
context.Selections.InsertOnSubmit((Selection) selection);
context.SubmitChanges();
}
}

最后是我的 XML 映射:

  <Table Name="dbo.Markets" Member="Markets">
<Type Name="Market">
<Column Name="MID" Member="MID" Storage="mID" DbType="Int NOT NULL" IsPrimaryKey="true" IsDbGenerated="true" AutoSync="OnInsert" />
<Association Name="FK_Market-Selections" Member="Selections" Storage="selections" ThisKey="MID" OtherKey="MID" DeleteRule="NO ACTION" />
</Type>
</Table>

<Table Name="dbo.Selections" Member="Selections">
<Type Name="Selection">
<Column Name="SID" Member="SID" Storage="sID" DbType="Int NOT NULL" IsPrimaryKey="true" IsDbGenerated="true" AutoSync="OnInsert" />
<Column Name="MID" Member="MID" Storage="mID" DbType="Int NOT NULL" />
<Association Name="FK_Market-Selections" Member="Market" Storage="market" ThisKey="MID" OtherKey="MID" IsForeignKey="true" />
</Type>
</Table>

那么,有人能指出我正确的方向吗?我已经找了好几个小时了...

编辑:

这是我的测试失败的堆栈跟踪:

at System.Data.Linq.ChangeTracker.StandardChangeTracker.StandardTrackedObject.SynchDependentData()
at System.Data.Linq.ChangeProcessor.ValidateAll(IEnumerable`1 list)
at System.Data.Linq.ChangeProcessor.SubmitChanges(ConflictMode failureMode)
at System.Data.Linq.DataContext.SubmitChanges(ConflictMode failureMode)
at System.Data.Linq.DataContext.SubmitChanges()
at BetMax.DataModel.Repository.AddSelection(ISelection selection) in Repository.cs: line 68
at BetMax.DataModel.Test.ModelTest.AddSelectionShouldAddSelectionToMarket() in ModelTest.cs: line 65

还有我的 GetMarket 方法:

    public IMarket GetMarket(int MID)
{
Market market;
using (MarketsDataContext context = MarketsDataContext.GetContext())
{
market = context.Markets.Single(m => m.MID == MID);
}
return market;
}

编辑2:

嗯,添加

DeleteOnNull="true"

XML 映射中的 Selections 外键已删除了外键错误,但现在我在 Selections 的一个子对象上得到了一个 null 引用,表示它对 Selection 的引用为 null,即使 Selection 正在初始化时没有使用任何它的变量集(外键之外)。我什至尝试创建一个子对象,并正确设置其引用,但仍然收到此错误:

System.NullReferenceException: Object reference not set to an instance of an object.
at BetMax.DTO.Price.set_Selection(Selection value) in Price.cs: line 25
at System.Data.Linq.Mapping.PropertyAccessor.Accessor`3.SetValue(ref T instance, V value)
at System.Data.Linq.Mapping.MetaAccessor`2.SetBoxedValue(ref Object instance, Object value)
at System.Data.Linq.ChangeProcessor.ClearForeignKeysHelper(MetaAssociation assoc, Object trackedInstance)
at System.Data.Linq.ChangeProcessor.ClearForeignKeyReferences(TrackedObject to)
at System.Data.Linq.ChangeProcessor.PostProcessUpdates(List`1 insertedItems, List`1 deletedItems)
at System.Data.Linq.ChangeProcessor.SubmitChanges(ConflictMode failureMode)
at System.Data.Linq.DataContext.SubmitChanges(ConflictMode failureMode)
at System.Data.Linq.DataContext.SubmitChanges()
at BetMax.DataModel.Repository.AddSelection(ISelection selection) in Repository.cs: line 68
at BetMax.DataModel.Test.ModelTest.AddSelectionShouldAddSelectionToMarket() in ModelTest.cs: line 69

价格是另一个对象,其构造方式与选择与市场相关(1个选择有很多价格,1个市场有很多选择)等相同。

最佳答案

我猜问题出在你的测试方法上。您使用 DataContext 创建了一个存储库,但您使用另一个存储库进行了提交。

[Test]
public void AddSelectionShouldAddSelectionToMarket()
{
Market market = (Market) new Repository().GetMarket(1);

Selection selection = new Selection();
selection.Market = market;

new Repository().AddSelection(selection);

Assert.IsTrue(selection.SID > 0);
}

创建一个存储库并在测试方法中使用它。

[Test]
public void AddSelectionShouldAddSelectionToMarket()
{
Repository repository = new Repository();
Market market = (Market) repository.GetMarket(1);

Selection selection = new Selection();
selection.Market = market;

repository.AddSelection(selection);

Assert.IsTrue(selection.SID > 0);
}

关于.net - 使用嵌套 POCO 进行 LINQ To SQL 操作(插入/更新)所需的帮助,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/298162/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com