efcore: KeyAttribute specified on multiple properties creates an incorrect model

Steps to reproduce

The entities

    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<Attachment>();
            Recipients = new List<Recipient>();
        }

        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<Recipient>();
        }
			
		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<Recipient>().HasKey(t => new { t.MessageId, t.EmailAddressId });

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."

Further technical details

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:

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Comments: 15 (9 by maintainers)

Commits related to this issue

Most upvoted comments

@donkitchen We are in the final stages of releasing it, should be out next week

Verified that this is fixed in the 1.1.1 candidate build.