google-code-export / dataobjectsdotnet

Automatically exported from code.google.com/p/dataobjectsdotnet
0 stars 0 forks source link

Exception while deleting object with composite key #731

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
I have a Book and a BookTranslation class. BookTranslation uses Book and 
Language as a composite key.

[Serializable]
[HierarchyRoot]
public class Book : Entity
{
    public Book(string name)
    {
        Name = name;
    }

    [Field, Key]
    public Guid Id { get; private set; }

    [Field(Length = 128)]
    public string Name { get; set; }

    [Field, Association(PairTo = "Book", OnOwnerRemove = OnRemoveAction.Cascade, OnTargetRemove = OnRemoveAction.Clear)]
    public EntitySet<BookTranslation> Translations { get; private set; }
}

[Serializable]
[HierarchyRoot, KeyGenerator(KeyGeneratorKind.None)]
public class BookTranslation : Entity
{
    public BookTranslation(Book book, Language language, string name) : base(book, language)
    {
        Name = name;
    }

    [Field, Key(0)]
    public Book Book { get; private set; }

    [Field, Key(1)]
    public Language Language { get; private set; }

    [Field(Length = 128)]
    public string Name { get; set; }

(...)

}

When I delete the BookTranslation, I get this error message:

[NotSupportedException: Unable to set Key field 'Language' explicitly.]
   Xtensive.Storage.Entity.SystemBeforeSetValue(FieldInfo field, Object value) +406
   Xtensive.Storage.Persistent.SetFieldValue(FieldInfo field, Object value) +1665
   Xtensive.Storage.ReferentialIntegrity.RemovalProcessor.ProcessItems(IList`1 entities) +1659
   Xtensive.Storage.ReferentialIntegrity.RemovalProcessor.Remove(IEnumerable`1 entities) +2316
   Xtensive.Storage.Entity.Remove() +169

Original issue reported on code.google.com by t...@faktum.co on 8 Jul 2010 at 2:06

GoogleCodeExporter commented 9 years ago
Try using this declaration:

[Field, Key(1)]
[Association(OnTargetRemove = OnRemoveAction.Cascade)]
// Or: [Association(OnTargetRemove = OnRemoveAction.Deny)] // Must be default 
in this case?
public Language Language { get; private set; }

Original comment by alex.yakunin on 8 Jul 2010 at 4:19

GoogleCodeExporter commented 9 years ago

Original comment by alex.yakunin on 8 Jul 2010 at 4:19

GoogleCodeExporter commented 9 years ago
Actually I had a "brain bug" on this one. I thought the problem was on the 
relation between Book and BookTranslation. Now I realized that the exception 
says Language... I cannot get it to work by setting Association attribute on 
Language property as suggested, but if I add a pairto collection to the 
Language class (as below), it works:

[Field, Association(PairTo = "Language", OnOwnerRemove = OnRemoveAction.Deny, 
OnTargetRemove = OnRemoveAction.Clear)]
public EntitySet<BookTranslation> Books { get; private set; }

In my opinion it should also have worked with OnOwnerRemove = 
OnRemoveAction.Clear directly on the BookTranslation, but that does not work.k

Original comment by t...@faktum.co on 8 Jul 2010 at 5:26