下面是用C#编写的示例。我正在使用Visual Studio Express 2012 for Windows Phone为Windows Phone 8.0编写代码。
我有一个简单的数据库设计(两个主表和一个多对多链接表)
Ingredients(IngredientID,IngredientName)
Effects(EffectID,EffectName)
IngredientEffects(IngredientID,EffectID)当然,为了让外键正常工作,我在所有三个表中都设置了标准实体和关系
这适用于以下情况:-添加多个成分
添加多个效果记录添加多个IngredientEffects
当我尝试删除一个包含多个关联成分的效果记录时,我会查询所有相关的IngredientEffect记录,然后执行一个DeleteAllOnSubmit(list);SubmitChanges();这会抛出多个"System.InvalidOperationException“,并在IngredientEffects表的Ingredients.FKIngredientEffects实体关系的set操作中抛出消息"Sequence contains one element”。我尝试从这个列表中构建一个唯一记录的列表,这样我就知道我在要删除的列表中没有重复的记录,但得到的消息是相同的。
我知道这种错误发生在单一类型的查询中,但我没有使用任何一种类型的查询。我的查询通常运行如下:
var query = from item in db.IngredientEffects
where item.EffectID == targetEffectID
select item;我验证查询不是null,然后尝试从它填充一个列表,并在继续任何其他工作之前检查它是否不是null并且其中有记录。
我不知道为什么我会得到这个错误。我在related Table定义中使用以下实体关系语句:
配料:
private EntityRef<IngredientEffect> _ingredientEffect;
[Association(Storage = "_ingredientEffect", ThisKey = "IngredientID", OtherKey = "IngredientID", IsUnique = false, Name = "FK_Ingredients_IngredientEffect")]
public IngredientEffect IngredientEffects
{
get { return _ingredientEffect.Entity; }
set
{
try
{
if (value != _ingredientEffect.Entity)
{
NotifyPropertyChanging("IngredientEffects");
_ingredientEffect.Entity = value;
NotifyPropertyChanged("IngredientEffects");
}
}
catch (Exception exc)
{
Debug.WriteLineIf(Debugger.IsAttached, "AlchemistDB.Ingredients.FKIngredientEffects(set) Exception: " + exc.Message);
//throw new Exception("AlchemistDB.Ingredient.FKIngredientEffects(set) failed.", exc);
}
}
}效果:
private EntityRef<IngredientEffect> _effectIngredients;
[Association(Storage = "_effectIngredients", ThisKey = "EffectID", OtherKey = "EffectID", IsUnique = false, Name = "FK_Effect_IngredientEffect")]
public IngredientEffect EffectIngredients
{
get { return _effectIngredients.Entity; }
set
{
try
{
NotifyPropertyChanging("EffectIngredients");
_effectIngredients.Entity = value;
NotifyPropertyChanged("EffectIngredients");
}
catch (Exception exc)
{
Debug.WriteLineIf(Debugger.IsAttached, "AlchemistDB.Effect.FKEffectIngredients(set) Exception: " + exc.Message);
//throw new Exception("AlchemistDB.Effect.FKEffectIngredients(set) failed.", exc);
}
}
}IngredientEffects:
private EntityRef<Ingredient> _ingredients;
[Association(Storage = "_ingredients", ThisKey = "IngredientID", OtherKey = "IngredientID", IsUnique = false, IsForeignKey = true, Name = "FK_Ingredients_IngredientEffect")]
public Ingredient Ingredients
{
get { return this._ingredients.Entity; }
set
{
try
{
if (value != _ingredients.Entity)
{
NotifyPropertyChanging("Ingredients");
this._ingredients.Entity = value;
NotifyPropertyChanged("Ingredients");
}
}
catch (Exception e)
{
Debug.WriteLineIf(Debugger.IsAttached, "AlchemistDB.IngredientEffect.FKIngredients(set) exception:" + e.Message);
throw new Exception("AlchemistDB.IngredientEffect.FKIngredients(set) failed.", e);
}
}
}
private EntityRef<Effect> _effects;
[Association(Storage = "_effects", ThisKey = "EffectID", OtherKey = "EffectID", IsUnique = false, IsForeignKey = true, Name = "FK_Effect_IngredientEffect")]
public Effect Effects
{
get { return this._effects.Entity; }
set
{
try
{
if (value != _effects.Entity)
{
NotifyPropertyChanging("Effects");
this._effects.Entity = value;
NotifyPropertyChanged("Efffects");
}
}
catch (Exception e)
{
Debug.WriteLineIf(Debugger.IsAttached, "AlchemistDB.IngredientEffect.FKEffects(set) exception:" + e.Message);
throw new Exception("AlchemistDB.IngredientEffect.FKEffects(set) failed.", e);
}
}
}我们将非常欢迎您提供任何帮助!
谢谢,-Mark
发布于 2014-07-31 04:50:10
如果多对多表中的任何外键的set声明具有条件逻辑(例如:
if (value!=_myprivateproperty)
{
NotifyPropertyChanging("MyPublicPropertyName");
_myprivateproperty = value;
NotifyPropertyChanged("MyPublicPropertyName");
}每次删除表中记录时,都会导致LINQ抛出此异常。去掉条件,直接使用:
NotifyPropertyChanging("MyPublicPropertyName");
_myprivateproperty = value;
NotifyPropertyChanged("MyPublicPropertyName");仍然不确定为什么会发生这种情况,但对我来说,这似乎是LINQ- to -SQL本身的一个错误。
https://stackoverflow.com/questions/25028344
复制相似问题