以下示例在C#中。我正在使用Visual Studio Express 2012 for Windows Phone编写Windows Phone 8.0。
我有一个简单的数据库设计(两个主表和一个多对多链接表)
Ingredients(IngredientID,IngredientName)
Effects(EffectID,EffectName)
IngredientEffects(IngredientID,EffectID)
所以当然要使外键工作我在所有三个表中都设置了标准的实体和关系
这适用于以下情况:
- 添加多种成分
- 添加多个效果
- 添加多种成分效果
- 删除成分(及其所有相关链接记录)
- 删除IngredientEffect记录
- 删除效果记录...但仅限于未链接到任何成分
当我尝试删除链接了多个成分的效果记录时,我查询所有相关的IngredientEffect记录,然后执行DeleteAllOnSubmit(list);的SubmitChanges(); 这会抛出多个“System.InvalidOperationException”,并在IngredientEffects表中的Ingredients.FKIngredientEffects实体关系的set操作中抛出“Sequence包含多个元素”的消息。我试图从这个列表中建立一个UNIQUE记录列表,所以我知道我在列表中没有重复的记录被删除但得到相同的消息。
我知道单一类型查询会发生此错误,但我没有使用其中任何一种。我的查询通常如下:
var query = from item in db.IngredientEffects
where item.EffectID == targetEffectID
select item;
我验证查询不是null,然后尝试从中填充列表,并检查它是否为空并且在继续执行任何其他工作之前在其中包含记录。
我不知道为什么会收到此错误。我在相关表定义中使用以下实体关系语句:
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);
}
}
}
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
答案 0 :(得分:0)
如果多对多表中的任何外键设置声明具有条件逻辑(例如:
if (value!=_myprivateproperty)
{
NotifyPropertyChanging("MyPublicPropertyName");
_myprivateproperty = value;
NotifyPropertyChanged("MyPublicPropertyName");
}
每次删除该表中的记录时,都会导致LINQ抛出此异常。摆脱条件,只需使用:
NotifyPropertyChanging("MyPublicPropertyName");
_myprivateproperty = value;
NotifyPropertyChanged("MyPublicPropertyName");
仍然不确定为什么会发生这种情况,但对我而言,这似乎是LINQ-to-SQL本身的一个错误。