Bailey Miller
Bailey Miller

Reputation: 1523

Entity framework cannot seem to create relationship

The entity framework isn't super new to me, however is confusing as I continue to expand my data models. I am attempting to create a class that has an array of another class. Class A or County.cs has a list of Class B or Product.cs

I cannot seem to create write these classes in a way that when you ask for context.counties you also get the array of products attached to it.

Class A or County.cs

public class County
{
    [Key]
    public int ID { get; set; }

    public String Name { get; set; }

    public List<Product> Products { get; set; } = new List<Product>();

    [NotMapped]
    public DateTime firstAppearance {
        get {
            var data = (from obj in Products orderby obj.Date descending select obj.Date).FirstOrDefault();

            if (this.softwareIn)
            {
                return data;
            }
            else
            {
                var date = new DateTime(1,1,1);
                return date;
            }
        }
        set {
            this.firstAppearance = value;
        }
    }

    [NotMapped]
    public bool softwareIn {
        get {
            return Products.Count() >= 1;
        }
        set {
            this.softwareIn = value;
        }
    }
}

Class B or Product.cs

public class Product
{
    [Key]
    public int ID { get; set; }

    public String Name { get; set; }

    public DateTime Date { get; set; }

    [NotMapped]
    public DateTime DateUtc {
        get {
            return getUtcDate();
        }
        set {
            this.DateUtc = value;
        }
    }

    public DateTime getUtcDate() {
        return this.Date.ToUniversalTime();
    }

}

I just don't understand and haven't created enough of 1:M relations in the entity framework. Why cannot I do something like this and have it work all the time? The first time I run this I get the type of data I expect, the xx county has a product. However if I remove all this and just return the context.counties I get nothing in the products array.

[Route("Counties")]
    public object GetCounties() {

        var data = new County() {
            Name = "xxx",
        };

        data.Products.Add(new Product() { Name="Cool Software", Date = DateTime.Now});

        db.Counties.Add(data);

        db.SaveChanges();

        var da = db.Counties.ToList();

        return db.Counties;
    }

Upvotes: 0

Views: 497

Answers (1)

Julian Wilson
Julian Wilson

Reputation: 2470

The reason you have having this issue is because the foreign keys are not correctly configured. Take a look at your database and look at the foreign keys. For Entity Framework to understand the relationships properly, you must mark related entities as virtual. So do this:

public virtual List<Product> Products { get; set;}

And then in your Product class add the navigation property back to the parent County:

public virtual County County { get; set;}

I found this tutorial really good:

http://www.entityframeworktutorial.net/code-first/configure-one-to-many-relationship-in-code-first.aspx

Hope this helps.

Upvotes: 2

Related Questions