Hello,

subject


Updated at 25.11.2009 9:20:20

Thanks, but dont work.

[HierarchyRoot]
    public class Person : Entity
    {
        [Field, Key]
        public int Id
        {
            get; private set;
        }
    }

    [HierarchyRoot]
    public class Book : Entity
    {
        [Field, Key]
        public int Id
        {
            get;
            private set;
        }
    }

    [HierarchyRoot]
    public class BookReview : Entity
    {
        [Field, Key(1)]
        public Person Reviewer { get; private set; }

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

        [Field(Length = 4096)]
        public string Text { get; set; }

        public BookReview(Book book, Person reviewer)
            : base(book, reviewer)
        { }
    }

When build domain throws exception "Default generator can serve hierarchy with exactly one key field."

This thread was imported from our support forum. The original discussion may contain more detailed answer. Original topic by Alexey Abretov.

asked Nov 25 '09 at 08:58

Editor's gravatar image

Editor
46144156157


One Answer:

Hello,

Here is the sample with multicolumn key:

[HierarchyRoot]
public class BookReview : Entity
{
  [Field, Key(1)]
  public Person Reviewer { get; private set; }

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

  [Field(Length = 4096)]
  public string Text { get; set; }

  public BookReview(Book book, Person reviewer)
    : base(book, reviewer)
  {}
}

Note how Key(1), Key(0) attributes are used to set the position of particular field in multicolumn key.


Sorry, forgot to say that you should indicate that default key generator must not be used for this class, as you set key fields by yourself in constructor:

[KeyGenerator(KeyGeneratorKind.None)]  <!-- Look here
    [HierarchyRoot]
    public class BookReview : Entity
    {
        [Field, Key(1)]
        public Person Reviewer { get; private set; }

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

        [Field(Length = 4096)]
        public string Text { get; set; }

        public BookReview(Book book, Person reviewer)
            : base(book, reviewer)
        { }
    }

answered Nov 25 '09 at 09:07

Dmitri%20Maximov's gravatar image

Dmitri Maximov
22111211

Have this helped?

// Reviewing the posts ;)

(Nov 25 '09 at 09:07) Alex Yakunin Alex%20Yakunin's gravatar image

Alexey Abretov wrote: v4.1.0 23.11.2009

(Nov 25 '09 at 09:07) Editor Editor's gravatar image
Your answer
Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!
toggle preview

Subscription:

Once you sign in you will be able to subscribe for any updates here

Tags:

×569

Asked: Nov 25 '09 at 08:58

Seen: 1,168 times

Last updated: Nov 25 '09 at 08:58

powered by OSQA