C#C
C#13mo ago
surwren

My constructor sets the attribute, so why is EFcore complaining?

why is EFCore complaining?

Required member 'UpMedia.OwnerId' must be set in the object initializer or attribute constructor.
Required member 'UpMedia.Owner' must be set in the object initializer or attribute constructor.

Here:

var upMedia = new UpMedia(upMediaDto.Type, user, upMediaDto.Bucket, upMediaDto.Path);
My code is

 try
 {
     int OnaId = await HttpHandler.GetOnaIdAsync(client, userInfoUri, token);
     var user = await _userService.GetUserByOnaIdAsync(OnaId);
     if (user == null)
     {
         user = await _userService.CreateUserByOnaIdAsync(OnaId);
         //return NotFound("User not found. Call the \"Ona/Token\" Get/Put methods in api/Users first");
     }
     var upMedia = new UpMedia(upMediaDto.Type, user, upMediaDto.Bucket, upMediaDto.Path);
     var createdUpMedia = await _upMediaService.CreateUpMediaAsync(upMedia);
     return Ok(UpMediaMapper.ToUpMediaDto(createdUpMedia));
 ...


   public class UpMedia : BaseUpload
   {
       [Key]
       [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
       public Guid Id { get; set; }
       [Required]
       public MediaType Type { get; set; } // Metadata

       // Navigation property        
       public required Guid OwnerId { get; set; }
       public required User Owner { get; set; }  // must always be present

       public ICollection<Post> Posts { get; set; }

       public UpMedia() : base()
       {
           Posts = new HashSet<Post>();
       }
       public UpMedia(MediaType Type, User Owner, string Bucket, string Path) : base(Bucket, Path)
       {
           this.Type = Type;
           this.OwnerId = Owner.Id;
           this.Owner = Owner;
           this.Posts = new HashSet<Post>();
       }

   }


???????
Was this page helpful?