question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

KeyAttribute specified on multiple properties creates an incorrect model

See original GitHub issue

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:

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:15 (9 by maintainers)

github_iconTop GitHub Comments

1reaction
AndriySvyrydcommented, Mar 2, 2017

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

1reaction
analogrelaycommented, Feb 23, 2017

Verified that this is fixed in the 1.1.1 candidate build.

Read more comments on GitHub >

github_iconTop Results From Across the Web

KeyAttribute Class (System.ComponentModel. ...
Denotes one or more properties that uniquely identify an entity. ... Property, AllowMultiple=false, Inherited=true)] public sealed class KeyAttribute : ...
Read more >
"[Key]" attribute supposed to set the column as primary but ...
I have a class already defined, later when I tried to set the primary key by putting [Key] attribute, the Add-migration command does...
Read more >
Error: ForeignKeyAttribute on property is not valid
'The ForeignKeyAttribute on property 'Image' on type 'Models.ContactModel' is not valid. The foreign key name 'ImageId' was not found on the ...
Read more >
The ForeignKey Attribute
The ForeignKey attribute is used to specify which property is the foreign key in a relationship. In the following example, the AuthorFK property...
Read more >
React key attribute: best practices for performant lists
Looking into how React "key" attribute works, how to use it correctly, how to improve performance of lists with it, and why array...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found