序列包含多个元素…在LINQ's DeleteAllOnSubmit

本文关键字:DeleteAllOnSubmit LINQ 包含多 元素 | 更新日期: 2023-09-27 18:15:26

下面的例子是用c#编写的。我正在使用Visual Studio Express 2012为Windows Phone编写Windows Phone 8.0代码。

我有一个简单的数据库设计(两个主表和一个多对多链接表)

Ingredients(IngredientID,IngredientName)
Effects(EffectID,EffectName)
IngredientEffects(IngredientID,EffectID)

当然,为了使外键工作,我在所有三个表中都设置了标准实体和关系

这适用于以下情况:-添加多种成分
-添加多个效果
-添加多个成分效果
-删除一个成分(和所有相关的链接记录)
-删除一条配料效果记录
-删除效果记录…但前提是它没有链接到任何成分

当我试图删除一个效果记录与多个成分链接到它,我查询所有相关的成份,然后做DeleteAllOnSubmit(列表);SubmitChanges ();这会抛出多个"System"。在ingredient的set操作中抛出InvalidOperationException,消息为"Sequence包含多个元素"。fkingredienteeffects成分表中的实体关系。我试图从这个列表中建立一个唯一记录的列表,所以我知道我在列表中没有重复的记录被删除,但得到相同的消息。

我明白这个错误发生在单一类型查询,但我不使用任何这些。我的查询通常是这样运行的:

var query = from item in db.IngredientEffects 
                    where item.EffectID == targetEffectID 
                    select item;

我验证查询不为空,然后尝试从它填充一个列表,并检查它是否不为空并且在继续任何其他工作之前有记录。

我不知道为什么我得到这个错误。我在相关的表定义中使用了以下实体关系语句:

<标题>成份:
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);
        }
    }
}

欢迎提供任何帮助!

谢谢,div标记

如果多对多表中任何外键的集合声明具有条件逻辑(如:

)
if (value!=_myprivateproperty)
{
    NotifyPropertyChanging("MyPublicPropertyName");
    _myprivateproperty = value;
    NotifyPropertyChanged("MyPublicPropertyName");
}

将导致LINQ抛出这个异常,每次该表中的一条记录被删除。去掉条件句,直接用:

NotifyPropertyChanging("MyPublicPropertyName");
_myprivateproperty = value;
NotifyPropertyChanged("MyPublicPropertyName");

仍然不确定为什么会发生这种情况,但对我来说,这似乎是LINQ-to-SQL本身的错误。

序列包含多个元素…在LINQ's DeleteAllOnSubmit

相关文章:
  • 没有找到相关文章