开发者

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

开发者 https://www.devze.com 2023-03-05 23:10 出处:网络
I have an object with a non-nullable property. class Noteboo开发者_JS百科k { Blueprint Blueprint { get; set; }

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

精彩评论

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