Hi, I have class:

    [Serializable]
    [HierarchyRoot]
    [Index("Parent", Unique = true, IncludedFields = new[] { "Name" }, Name = "IXU_Parent_Name")]
    public class Folder: Entity
    {
        [Field, Key]
        public Guid Id { get; private set; }

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

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

        [Field]
        public Folder Parent { get; set; }
    }

and some table:

CREATE TABLE [dbo].[Folder](
    [Id] [uniqueidentifier] NOT NULL,
    [Name] [nvarchar](255) NULL,
    [Parent.Id] [uniqueidentifier] NULL,
 CONSTRAINT [PK_Folder] PRIMARY KEY CLUSTERED ([Id] ASC)
)

and can't create unique combined index on fields Name and Parent.Id get result:

  1. Create single field Index with name as field
  2. or if I try write "Parent.Id" as fieldName get exception: "Item with name 'Folder.FK_Parent.TYPED' already exists in 'Folder.Indexes'."

asked Sep 14 '10 at 10:53

dj_raphael's gravatar image

dj_raphael
5224

edited Sep 14 '10 at 13:44

Alex%20Yakunin's gravatar image

Alex Yakunin
29714412


One Answer:

IndexAttribute.IncludedFields purpose is different: this property is used to include additional fields into the index. Such fields aren't used in index key part, so query optimizer won't be able to use them in seek operations; but it will use them instead of joining actual table rows, if information stored in index is enough to serve the query.

This MSDN article fully describes the concept.

Returning back to the original problem: you need the following index declaration:

 [Index("Parent", "Name", // Order is "Parent", "Name"
   Name = "IXU_Parent_Name", IsUnique = true)]

I.e. first you enumerate the names of fields to include. "Parent" is name of the parent field, so this part will be mapped to appropriate foreign key columns (in your case it is just one: "Parent.Id").

answered Sep 14 '10 at 13:57

Alex%20Yakunin's gravatar image

Alex Yakunin
29714412

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:

×574
×23
×16

Asked: Sep 14 '10 at 10:53

Seen: 5,704 times

Last updated: Sep 14 '10 at 13:57

powered by OSQA