Error "EntityType has no key defined" for composite keys

ef-code-first entity-framework-6

Question

Entity framework v6.1.3 is what I'm attempting to use to link my data to, however I keep receiving the error EntityType doesn't have a specified key. Define this EntityType's key.. I've got a composite key.

I've tried the following methods:

public class CommunicationCollection
{
    [Key, Column(Order = 0)]
    [ForeignKey("FK_CommunicationCollection_Communication")]
    public Guid CommunicationId;

    [Key, Column(Order = 1)]
    [ForeignKey("FK_CommunicationCollection_Collection")]
    public Guid CollectionId;

}

also that

public class CommunicationCollection
    {
        [Key, Column(Order = 0)]
        [ForeignKey("FK_CommunicationCollection_Communication")]
        public Guid CommunicationId;

        [Key, Column(Order = 1)]
        [ForeignKey("FK_CommunicationCollection_Collection")]
        public Guid CollectionId;

        public virtual Communication Communication { get; set; }
        public virtual Collection Collection { get; set; }

    }

as well as this

public class CommunicationCollection
    {
        [Key, Column(Order = 0)]
        public Guid CommunicationId;

        [Key, Column(Order = 1)]
        public Guid CollectionId;    
    }

and I have in the DB

CREATE TABLE [CommunicationCollection](
    [CommunicationId] [uniqueidentifier] NOT NULL,
    [CollectionId] [uniqueidentifier] NOT NULL,
 CONSTRAINT [PK_CommunicationCollection] PRIMARY KEY CLUSTERED 
(
    [CommunicationId] ASC,
    [CollectionId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 80) ON [PRIMARY]
) ON [PRIMARY]

GO

ALTER TABLE [CommunicationCollection]  WITH CHECK ADD  CONSTRAINT [FK_CommunicationCollection_Collection] FOREIGN KEY([CollectionId])
REFERENCES [Collection] ([CollectionId])
GO

ALTER TABLE [CommunicationCollection] CHECK CONSTRAINT [FK_CommunicationCollection_Collection]
GO

ALTER TABLE [CommunicationCollection]  WITH CHECK ADD  CONSTRAINT [FK_CommunicationCollection_Communication] FOREIGN KEY([CommunicationId])
REFERENCES [Communication] ([CommunicationId])
GO

ALTER TABLE [CommunicationCollection] CHECK CONSTRAINT [FK_CommunicationCollection_Communication]
GO

Do you know what I'm missing? A big thank you!

1
0
2/13/2017 4:00:43 PM

Accepted Answer

ZZZ_tmp
2
2/13/2017 4:00:43 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow