3

I have an Entity Framework model:

public class Application
{
    [Key]
    public int ApplicationID { get; set; }
    public int PatentID { get; set; }

    ...

    //------------------------------------
    public string ApplicationNumber { get; set; }
    public string Priority { get; set; }
    public List<ApplicationPayment> Payments { get; set; } 
                          = new List<ApplicationPayment>();
}

and payment's model:

public class ApplicationPayment
{
    [Key]
    public int PaymentID { get; set; }
    public string PaymentName { get; set; }
    public float Amount { get; set; }
    public int PayNumber { get; set; }
    public DateTime Date { get; set; } = new DateTime(2017, 12, 1);
    public float TopicPart { get; set; }
}

Entity Framework creates additional foreign keys for me in ApplicationPayment model Application_ApplicationID.

I add a new instance in ApplicationPayment table that has number of the existing Application:sql models

But when I try to display this ApplicationPayment's table this returns the empty table.

I tried to add ApplicationPayment manually through SQL Server Management Studio and via fake-request. New line added but the list of ApplicationPayment is still empty.

Fake-request:

    [HttpPut]
    public void CreateApplicationPayment(int? id)
    {
        ApplicationPayment appPayment = new ApplicationPayment()
        {
            Amount = 80.0f,
            Date = new DateTime(2017, 10, 25),
            PaymentName = "payment",
            PayNumber = 30,
            TopicPart = 20
        };

        Application application = db.Applications.Find(id);

        application.Payments.Add(appPayment);
        db.SaveChanges();

    }
7
  • See Loading Related Entities Commented May 22, 2017 at 17:42
  • U are using PUT ? Well you need to map the data as to modal .. Commented May 22, 2017 at 17:45
  • 1
    stackoverflow.com/questions/20625991/… Commented May 22, 2017 at 17:49
  • why do you have the "= new ..." lines in there? I don't think models are really the place for them Commented May 22, 2017 at 17:55
  • 1
    To clarify on Maxim's comment: ApplicationPayment needs to reference a specific Application. Try adding: public Application Application {get;set;} to the ApplicationPayment class Commented May 22, 2017 at 18:02

1 Answer 1

4

Your collection property needs to be virtual if you want EF to automatically populate it:

public virtual List<ApplicationPayment> Payments { get; set; }

Also, if you're using EF 6 or previous, you'll need to make the type of that property ICollection<ApplicationPayment>, rather than List<ApplicationPayment>. I think EF Core relaxed this restriction, but I'm not sure. So, if you still have issues, change it there as well.

However, this is what's called lazy-loading, and it's not ideal in most scenarios. Additionally, if you're using EF Core, it still won't work, because currently EF Core does not support lazy loading. The better method is to eagerly load the relationship. This is done by using Include in your LINQ query:

Application application = db.Applications.Include(m => m.ApplicationPayments).SingleOrDefault(m => m.Id == id);

This will cause EF to do a join to bring in the related ApplicationPayments. You need to then use SingleOrDefault rather than Find, as Find doesn't work with Include. (Find looks up the object in the context first, before hitting the database, and as a result, cannot account for related items being available.)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.