【发布时间】:2014-07-30 03:35:09
【问题描述】:
以下示例使用 C#。我正在使用 Visual Studio Express 2012 for Windows Phone 为 Windows Phone 8.0 编写代码。
我有一个简单的数据库设计(两个主表和一个多对多链接表)
Ingredients(IngredientID,IngredientName)
Effects(EffectID,EffectName)
IngredientEffects(IngredientID,EffectID)
所以当然要让外键正常工作,我在所有三个表中都设置了标准实体和关系
这适用于以下情况:
- 添加多种成分
- 添加多个效果
- 添加多个成分效果
- 删除成分(及其所有相关链接记录)
- 删除成分效果记录
- 删除效果记录......但前提是它没有链接到任何成分
当我尝试删除链接到多个成分的效果记录时,我查询所有关联的成分效果记录,然后执行 DeleteAllOnSubmit(list);提交更改(); 这会引发多个“System.InvalidOperationException”,其中“序列包含多个元素”的消息被抛出到 IngredientEffects 表中的 Ingredients.FKIngredientEffects 实体关系的集合操作中。我试图从这个列表中构建一个 UNIQUE 记录的列表,所以我知道列表中没有重复的记录被删除,但得到了相同的消息。
我了解单类型查询会发生此错误,但我没有使用其中任何一个。我的查询通常运行如下:
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);
}
}
}
欢迎提供任何帮助!
谢谢, -马克
【问题讨论】:
标签: c# entity-framework exception windows-phone-8 linq-to-sql