Search code examples
asp.net-mvcforeign-keys

ASP.NET MVC - 2 tables, foreign key to 2nd table, Guid is present, underneath columns are null


Sorry for the 'blurry" question, but I don't know how to make it more clear.

Problem is the following: I have a table with Files and another table with PaymentMethods.

In Files, I have a FK column PaymentMethodID. When I select a PaymentMethod, the ID is neatly filled in the column Files.PaymentMethodID as expected, but PaymentMethod remains null.

I thought I should be able to access columns from PaymentmentMethods as File.PaymentMethod.PaymentMethod etc.

Any ideas? I did this before, but somehow, this time it just fails ... must be something I'm overlooking.

Entity classes:

public class Files
{
     public Guid FileID { get; set; }
     public Guid? PaymentMethodID { get; set; }

     [ForeignKey(nameof(PaymentMethodID))]
     [Display(Name = "Method")]
     public PaymentMethods? PaymentMethod { get; set; }
}

public class PaymentMethods
{
     public Guid PaymentMethodID { get; set; }

     [Display(Name = "Method")]
     [Remote("Duplicate", "PaymentMethods", HttpMethod = "POST", ErrorMessage = "This method already exists.")]
     public string PaymentMethod { get; set; }
}

Controller:

[HttpPost, ActionName("EditPayment")]
[ValidateAntiForgeryToken]
public ActionResult EditPost([Bind("FileID,RowVersion,CreatedBy,CreatedDate," +
    "Paid,PaymentMethodID,PaymentDate")] Files model)
{
    try
    {
        _context.Entry(fileToUpdate).OriginalValues["RowVersion"] = model.RowVersion;

        var method = _context.PaymentMethods.Find(model.PaymentMethodID);

        if (model.PaymentMethodID == null) 
        { 
            fileToUpdate.Paid = false; 
        }
        else 
        { 
            fileToUpdate.Paid = true; 
        }

        fileToUpdate.PaymentMethodID = model.PaymentMethodID;
        fileToUpdate.PaymentMethod = method;             // <- This is just to try if it's solved
        fileToUpdate.PaymentDate = model.PaymentDate;

        _context.SaveChanges();

        return View(fileToUpdate);
    }
    catch (DbUpdateConcurrencyException ex)
    {
        //blabla
    }
    catch (DataException /* dex */)
    {
       //blabla
    }

    return View(fileToUpdate);
}

Context:

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    base.OnModelCreating(modelBuilder);

    //blabla

    modelBuilder.Entity<Files>()
       .HasOne(cf => cf.Customers)
       .WithMany(c => c.Files)
       .OnDelete(DeleteBehavior.Cascade);

    modelBuilder.Entity<Files>()
        .HasKey(cf => new { cf.FileID });

    modelBuilder.Entity<Files>()
        .Property(cf => cf.RowVersion).IsConcurrencyToken();

    modelBuilder.Entity<PaymentMethods>()
        .HasKey(pm => new { pm.PaymentMethodID });

    modelBuilder.Entity<PaymentMethods>()
        .Property(pm => pm.RowVersion).IsConcurrencyToken();
}

File.PaymentMethod.PaymentMethod should be available


Solution

  • file = from s in _context.Files
       .Include(p => p.PaymentMethod)
       where s.Customers.CustomerID == customerId
       select s;
    

    .Include(p => p.PaymentMethod) was forgotten.