Quantcast
Channel: Web API
Viewing all articles
Browse latest Browse all 4850

How to set up a relation between a property and a collection in the same entity?

$
0
0

My idea is to upload all files in one table and store their Ids in the related tables. for example,

public class ApplicationUser : IdentityUser {
        [Required, StringLength (50)]
        [Display (Name = "First Name")]
        public string FirstName { get; set; }
        public long UserAvatarId { get; set; }
        public virtual ICollection<FileUpload> UploadedFiles { get; set; }
}

UserAvatarId is just a property that is related to the File Upload TableRecordId.

 public class FileUpload {
        [Key, DatabaseGenerated (DatabaseGeneratedOption.None)]
        public long RecordId { get; set; }
        public string UserId { get; set; }
        public ApplicationUser User { get; set; }
        public long RecordKey { get; set; }
        public long Length { get; set; }
        public string PhysicalPath { get; set; }
        public string Name { get; set; }
        public OffsetDateTime? LastModified { get; set; }
        public LocalDateTime? ValidUntil { get; set; }
        public FileCategory FileCategory { get; set; }

    }

my problem is that how can I tell EF core that if the record in the upload table is deleted to set UserAvatarId to null?

I tried to set up a navigation property like this but EF core gets confused about virtual ICollection<FileUpload>.

public FileUpload UserAvatar { get; set; }

builder.Entity<ApplicationUser> ()
.HasOne (c => c.UserAvatar)
.WithOne ()
.OnDelete (DeleteBehavior.SetNull);

Viewing all articles
Browse latest Browse all 4850

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>