Hi, I have class:

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

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

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

        public Folder Parent { get; set; }

and some table:

CREATE TABLE [dbo].[Folder](
    [Id] [uniqueidentifier] NOT NULL,
    [Name] [nvarchar](255) NULL,
    [Parent.Id] [uniqueidentifier] NULL,

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


edited Sep 14 '10 at 13:44

Alex%20Yakunin's gravatar image

Alex Yakunin

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

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


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



Asked: Sep 14 '10 at 10:53

Seen: 5,777 times

Last updated: Sep 14 '10 at 13:57

powered by OSQA