现在我正在使用城堡活动记录在asp-net上构建一个Web应用程序。当我试图保存具有多个关系的实体时,我得到了错误:“对象引用未保存的瞬态实例 - 在刷新之前保存瞬态实例。类型:SupBoardModel.Entities.Output,Entity:SupBoardModel.Entities.Output# 0" 。在网上搜索我发现了这个错误的原因和一些解决方案,但没有人为我工作。该关系已经有一个属性设置为Cascade = ManyRelationCascadeEnum.All,这是网络上的一个常见建议所以......这里有什么问题?有一段代码可以获得更多信息和理解:
//In some part of my application
State state = new State();
//Set some fields
//...
state.Outputs = (List<Output>)Session["outputs"]; //Collection filled on a web form but not saved yet
//Here is the error
state.SaveAndFlush(); // Booom!!!!
//Part of a definition of Output(child entity)
[Serializable, ActiveRecord(Table = "SUPB_OUTPUTS")]
public class Output : ActiveRecordBase<Output>
{
private int _id;
/// <summary>
/// Primary key
/// </summary>
[PrimaryKey(PrimaryKeyType.SeqHiLo, "OUTPUT_ID", SequenceName = "SEQ_OUTPUT_ID")]
public int Id
{
get { return _id; }
set { _id = value; }
}
private string _label;
/// <summary>
/// Output custom label
/// </summary>
[Property("OUTPUT_LABEL")]
public string Label
{
get { return _label; }
set { _label = value; }
}
private State _state;
/// <summary>
/// StateRef owner (An output is only available for one state)
/// </summary>
[BelongsTo("OUTPUT_ID_STATE", Lazy = FetchWhen.OnInvoke)]
public State StateRef
{
get { return _state; }
set { _state = value; }
}
}
// Part of a definition of State(parent entity)
[Serializable, ActiveRecord(Table = "SUPB_STATES")]
public class State : ActiveRecordBase<State>
{
private int _id;
/// <summary>
/// Primary key
/// </summary>
[PrimaryKey(PrimaryKeyType.SeqHiLo, "STATE_ID", SequenceName = "SEQ_STATE_ID")]
public int Id
{
get { return _id; }
set { _id = value; }
}
private string _name;
/// <summary>
/// StateRef name
/// </summary>
[Property("STATE_NAME")]
public string Name
{
get { return _name; }
set { _name = value; }
}
private string _description;
/// <summary>
/// StateRef description
/// </summary>
[Property("STATE_DESC")]
public string Description
{
get { return _description; }
set { _description= value; }
}
private IList<Output> _outputs;
/// <summary>
/// State outputs (Columns to display data)
/// </summary>
[HasMany(typeof(Output), Table = "SUPB_OUTPUTS", ColumnKey = "OUTPUT_ID_STATE", Lazy = true, Cascade = ManyRelationCascadeEnum.All)]
public IList<Output> Outputs
{
get { return _outputs; }
set { _outputs = value; }
}
}
这个错误让我发疯了。我希望这是一种在不保存每个输出之前保存状态的方法。 cascade属性对我来说没有变化,所有选项(All,AllDeleteOrfan,SaveUpdate)都给我相同的结果。这个案例很常见,并在http://docs.castleproject.org/%28X%281%29S%28znghcs55lveeljjvqg21vni4%29%29/Active%20Record.Getting%20Started.ashx中提到,但对我来说却是一个谜。任何人都可以帮助我吗?
由于 Menrique
答案 0 :(得分:0)
好的,我把Cascade = CascadeEnum.All放在输出的StateRef字段中,就像那样:
/// <summary>
/// StateRef owner (An output is only available for one state)
/// </summary>
[BelongsTo("OUTPUT_ID_STATE", Lazy = FetchWhen.OnInvoke, Cascade=CascadeEnum.All)]
public State StateRef
{
get { return _state; }
set { _state = value; }
}
它工作!因此,仅仅在父实体的关系hasMany中放入Cascade = ManyRelationCascadeEnum.All是不够的,在子实体中也是必要的。
由于 Menrique