开发者

Fluent nhibernate, delete a non-nullable property and replace it

I have an object with a non-nullable property.

class Noteboo开发者_JS百科k {
  Blueprint Blueprint { get; set; }
}

Mapping

// ...
NotebookMap() {
 // ...
 References(x => x.Blueprint)
  .Not.Nullable()
  .Cascade.All();
}

Okay, great. Wonderful.

What happens if I want to change the Blueprint attached to a notebook, and delete the old blueprint? This doesn't work..

notebook.Blueprint = // new blueprint code.;

That works fine... but then the old blueprint isn't deleted, it just hangs around in code happily and wastes space.

If I try this ..

session.Delete(notebook.Blueprint);

I get an error, because now the field is null (and it has to be non-nullable).

Is there any way around this problem?


How about something like this?

var oldBlueprint = notebook.Blueprint;
notebook.Blueprint = // new blueprint code;
session.Flush(); // might or might not be needed
session.Delete(oldBlueprint);

Of course, the blueprint may be referenced by another notebook, so the delete may still fail.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜