The entities
```C#
public class Recipient
{
[Key, ForeignKey("Message")]
[Column(Order = 0)]
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public int MessageId { get; set; }
[Key, ForeignKey("EmailAddress")]
[Column(Order = 1)]
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public int EmailAddressId { get; set; }
public RecipientType Type { get; set; }
public FlagType Flags { get; set; }
public MarkType Marked { get; set; }
public EmailAddress EmailAddress { get; set; }
public Message Message { get; set; }
}
public class Message
{
public Message()
{
Attachments = new List
Recipients = new List
}
public int Id { get; set; }
public DateTime Created { get; set; }
public string CreatedById { get; set; }
public MediagralUser CreatedBy { get; set; }
public DateTime Modified { get; set; }
public string ModifiedById { get; set; }
public MediagralUser ModifiedBy { get; set; }
public DateTime Accessed { get; set; }
public string AccessedById { get; set; }
public MediagralUser AccessedBy { get; set; }
[Required]
public string Subject { get; set; }
[Required]
public string Body { get; set; }
[Required]
[StringLength(50)]
public string Mime { get; set; }
[StringLength(100)]
public string Metadata { get; set; }
public int? InReplyToId { get; set; }
public Message InReplyTo { get; set; }
public /*System.Net.Security.EncryptionPolicy*/int Encrypted { get; set; }
public DateTime DateReceived { get; set; }
[Required]
public int FolderId { get; set; }
public Box Folder { get; set; }
public ICollection<Attachment> Attachments { get; set; }
public ICollection<Recipient> Recipients { get; set; }
}
public class EmailAddress
{
public EmailAddress()
{
Recipients = new List
}
public int Id { get;set; }
[Required]
[DataType(DataType.EmailAddress)]
public string Mail { get; set; }
public DateTime Created { get; set; }
public string CreatedById { get; set; }
public MediagralUser CreatedBy { get; set; }
public DateTime Modified { get; set; }
public string ModifiedById { get; set; }
public MediagralUser ModifiedBy { get; set; }
public DateTime Accessed { get; set; }
public string AccessedById { get; set; }
public MediagralUser AccessedBy { get; set; }
[StringLength(150)]
public string Label { get; set; }
public ICollection<Recipient> Recipients { get; set; }
}
the fluent declaration into the dbcontext
modelBuilder.Entity
### The issue
It's alright with the 1.0.0 of entityframwork and netcoreapp 1.1 and failed with the 1.1.0 when add a second entity in the collection.
"The instance of entity type 'Recipient' cannot be tracked because another instance of this type with the same key is already being tracked. When adding new entities, for most key types a unique temporary key value will be created if no key is set (i.e. if the key property is assigned the default value for its type). If you are explicitly setting key values for new entities, ensure they do not collide with existing entities or temporary values generated for other new entities. When attaching existing entities, ensure that only one entity instance with a given key value is attached to the context."
```
netcoreapp : 1.1
dotnet core : 1.1.0
EF Core version: 1.1.0
Operating system: debian 8.5
Visual Studio version: vs code 1.7.1
Other details about my project setup:
@branciat Can you show the code for "adding a second entity in the collection"?
To simplify the reproduction of error, I created a project on my github account TestCompositeKey.
Normally just change the version of entityframework to 1.0.0 in the project.json for it to work.
@branciat I was able to repro this. A workaround is to remove the Key annotations on Recipient, which are not used by EF and should be ignored, but there appears to be a problem ignoring them.
Notes for triage: this appears to be a model building regression involving data annotations from 1.0 to 1.1. The Recipient class has the following:
```C#
[Key, ForeignKey("Message")]
[Column(Order = 0)]
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public int MessageId { get; set; }
[Key, ForeignKey("EmailAddress")]
[Column(Order = 1)]
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public int EmailAddressId { get; set; }
The composite key is specified in the fluent API, so these annotations should have no effect. However, they result in a model with two additional keys:
EntityType: Recipient
Properties:
MessageId (int) Required PK FK ReadOnlyAfterSave 0 0 0 -1 0
EmailAddressId (int) Required PK FK ReadOnlyAfterSave 1 1 1 -1 1
Flags (FlagType) Required 2 2 -1 -1 -1
Marked (MarkType) Required 3 3 -1 -1 -1
Type (RecipientType) Required 4 4 -1 -1 -1
Navigations:
EmailAddress (
Message (
Keys:
MessageId
EmailAddressId, MessageId
MessageId, EmailAddressId PK
Foreign keys:
EmailAddressId -> EmailAddress.Id ToDependent: Recipients ToPrincipal: EmailAddress
MessageId -> Message.Id ToDependent: Recipients ToPrincipal: Message
Removing the Key annotations results in this model:
EntityType: Recipient
Properties:
MessageId (int) Required PK FK ReadOnlyAfterSave 0 0 0 -1 0
EmailAddressId (int) Required PK FK Index ReadOnlyAfterSave 1 1 1 -1 1
Flags (FlagType) Required 2 2 -1 -1 -1
Marked (MarkType) Required 3 3 -1 -1 -1
Type (RecipientType) Required 4 4 -1 -1 -1
Navigations:
EmailAddress (
Message (
Keys:
MessageId, EmailAddressId PK
Foreign keys:
EmailAddressId -> EmailAddress.Id ToDependent: Recipients ToPrincipal: EmailAddress
MessageId -> Message.Id ToDependent: Recipients ToPrincipal: Message
```
It's exact the table generated by this example is incorrect, I saw later that it created 2 unnecessary key in addition to the primary key. Once i deleted the constraints unnecessary the error continue to appear, but the keys annotations were still here.
I will test the workaround on my current project.
thanks
I hope that we keep the annotation keys, I find that the constraints are more readable at the entity level than at the level fluent api.
Fixed in 6b4539b
This patch bug is approved. Please use the normal code review process w/ a PR and make sure the fix is in the correct branch, then close the bug and mark it as done.
I have the same problem, how can I fix it?
@nicojmb the fix will ship in 1.1.1. In the meantime, remove the key attributes.
Verified that this is fixed in the 1.1.1 candidate build.
Damn just ran into this one as well.
Any idea when 1.1.1 will be out?
@donkitchen We are in the final stages of releasing it, should be out next week
Yep, like super duper soon.
Still not fixed in 1.1.1,
We need the [Key] to be present in a base class called "BaseEntity".
later one of the child entities can add another [Key] to make a composite key.
although deleting both keys in parent and in the child will solve the issue.
@zzis Composite keys cannot be specified using [Key] because it doesn't allow to specify the order.
Are you having issues even if you configure the key using .HasKey() ?