1

This is my class briefly:

public class UploadFile
{

    public int Id { get; set; }
    public TinyBlog2User User { get; set; } //this user is extend from IdentityUser
    public byte[] md5 { get; set; }
    public string Uri { get; set; }
    public string ThumbnialUri { get; set; }
}

Then I upload a file and save the file-url to database like this way:

 UploadFile uploadedFile = new UploadFile()
                        {
                            md5 = md5Value,
                            Uri = fileBlob.Uri.ToString(),
                            User = currentUser,
                            ThumbnialUri = thumbnailBlob.Uri.ToString()
                        };

                        _dbContext.uploadFiles.Add(uploadedFile);

                        _dbContext.SaveChanges();

But in my uploadFile table 'userId' column is always null. I am new comer to ASP.NET Core, can you tell me what is going wrong with my code? Thank you very much!

enter image description here

TanvirArjel
  • 30,049
  • 14
  • 78
  • 114
WangJi
  • 191
  • 2
  • 8
  • What do you mean by *"then it always be null"*? What operation? If reading from database, it won't be populated automatically - see [Loading Related Data](https://learn.microsoft.com/en-us/ef/core/querying/related-data). You'd probably need to use `Include` method. – Ivan Stoev Dec 28 '18 at 15:20
  • thanks for your answer, my mean is the column in database don't has value,it's null. I'm not good at English,I hope you don't mind. And I upload a picture to question.hope you can get my point. – WangJi Dec 28 '18 at 15:38

2 Answers2

0

In the extend from IdentityUser write

List<UploadFile> UploadFile {get; set;}

and in upload file

public int UserId {get; set;}

Here type should the the id type of User Table and property name should be TableName+Id

0

Write your UploadFile model as follows:

public class UploadFile
{

    public int Id { get; set; }
    public string UserId { get; set; } 
    public byte[] md5 { get; set; }
    public string Uri { get; set; }
    public string ThumbnialUri { get; set; }

    public ApplicationUser ApplicationUser { get; set; } //this user is extend from `IdentityUser`
}

Then in the DbConext configuration.

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
     modelBuilder.Entity<UploadFile>()
            .HasOne(uf => uf.ApplicationUser)
            .WithMany()
            .HasForeignKey(uf => uf.UserId);
}

Now run a brand new migration!

And modify your FileUpload method code as follows:

UploadFile uploadedFile = new UploadFile()
                    {
                        md5 = md5Value,
                        Uri = fileBlob.Uri.ToString(),
                        UserId = currentUser.Id, // Here set the current logged in UserId
                        ThumbnialUri = thumbnailBlob.Uri.ToString()
                    };

                    _dbContext.uploadFiles.Add(uploadedFile);

                    _dbContext.SaveChanges();

Note: If you need how to get current logged in UserId then Here it is

TanvirArjel
  • 30,049
  • 14
  • 78
  • 114