简体   繁体   中英

Entity Framework core many to many insert in navigation properties failed?

I'm starting with ef core - code first, and I have simple asp.net console app im which I have 3 tables and 1 join table for many to many relationship. This is how tables looks like:

public class Kupac
 {
    public int Id { get; set; }
    public String Ime { get; set; }
    public String Prezime { get; set; }
    public List<Racun> Racuni { get; set; }       
 }
public class Proizvod
 {
    public int Id { get; set; }
    public String Naziv { get; set; }
    public float Cijena { get; set; }
    public List<ProizvodRacun> ProizvodRacun { get; set; }
 }
public class ProizvodRacun
 {
    public int ProizvodId { get; set; }
    public Proizvod Proizvod { get; set; }
    public int RacunId { get; set; }       
    public Racun Racun { get; set; }
    public int Kolicina { get; set; }
 }
public class Racun
 {
    public int Id { get; set; }
    public List<ProizvodRacun> ProizvodRacun { get; set; }
    public int? KupacId {get; set;}
    public Kupac Kupac { get; set; }
    public bool Active { get; set; }
 }

I know that I should use ICollection instead of List but that's not problem right now I think. I have some data in tables "Kupac(Customer)" and "Proizvod(Product)". When I try to add something in this join table - "ProizvodRacun" I use this code:

using (MyContext context = new MyContext())
        {
            Kupac kupacc = context.Kupci.SingleOrDefault(k => k.Id == 2);
            Proizvod proizvod = context.Proizvodi.SingleOrDefault(p => p.Id == 5);
            Racun racun = new Racun();
            racun.Kupac = kupacc;
            ProizvodRacun proizvodRacun = new ProizvodRacun();
            proizvodRacun.Proizvod = proizvod;
            proizvodRacun.Racun = racun;
            proizvodRacun.Kolicina = 5;
            context.Add(proizvodRacun);
            context.SaveChanges();
        }

And this works! But when I try to do the same thing in other way navigation properties are not added, but records in tables are!! The other way is this:

Racun racun = null;
            Kupac tempKupac = null;
            ProizvodRacun proizvodRacun = new ProizvodRacun();
            tempKupac = context.Kupci.SingleOrDefault(k => k.Id == kupac.Id);
            if (tempKupac != null)
            {
                if (tempKupac.Racuni != null)
                {
                    racun = context.Racuni.SingleOrDefault(r => r.Active == true);
                    if (racun != null)
                    {
                        proizvodRacun.Racun = racun;
                        context.Add(proizvodRacun);
                        context.SaveChanges();
                    }
                    else
                    {
                        racun = new Racun();
                        racun.Active = true;
                        racun.Kupac = tempKupac;
                        racun.ProizvodRacun = new List<ProizvodRacun>();
                        proizvodRacun.Racun = racun;
                        context.Add(proizvodRacun);
                        context.SaveChanges();
                    }
                }
                else
                {
                    //FIRST TIME THIS CODE GET EXECUTED SO JUST WATCH THIS CASE
                    racun = new Racun();
                    racun.Kupac = tempKupac;
                    proizvodRacun.Proizvod = context.Proizvodi.SingleOrDefault(p => p.Id == products[i].Id);
                    proizvodRacun.Kolicina = j;// j -> option from code above that can't be seen...
                    proizvodRacun.Racun = racun;
                    context.Add(proizvodRacun);
                    context.SaveChanges();
                }
            }
            else
            {
                Console.WriteLine("Customer doesn't exist!");
            }

Please help me ...

Ok I found problem, I had to use .Include() when I queried "Kupac" , to include its "Racun"s. The "Racun" was always there just it wasn't loaded.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM