好吧,我一直在尝试将我的模型转换为使用 LINQ,但不想丢弃我当前的 DTO 及其分散在域中的接口。
我设法找到了这篇博客文章,它很好地概述了这个过程:
我设法让对象的记录检索正常工作,但是,由于我的模型的嵌套性质,我似乎无法为子对象添加工作。也就是说,如果我创建一个子对象,并设置对所需父对象的引用,LINQ to SQL 仍然会引发异常,说明子对象对父对象的引用为空。如果我尝试添加一个普通的旧父对象,它会成功,但直接添加子对象会失败
这是我失败的测试:
[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。
2个对象的相关部分:
[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 的其中一个子对象上获得了一个空引用,说它对 Selections 的引用为空,即使在未设置任何变量的情况下初始化 Selections (外键之外)。我什至尝试创建一个子对象,并正确设置它的引用,但仍然收到此错误:
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 个市场有很多选择)等等。