简体   繁体   English

实体框架核心在导航属性中多次插入失败?

[英]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. 我从ef core开始-首先编写代码,我有一个简单的asp.net控制台应用程序im,其中有3个表和1个连接表,用于多对多关系。 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. 我知道我应该使用ICollection而不是List,但是我认为现在这不是问题。 I have some data in tables "Kupac(Customer)" and "Proizvod(Product)". 我在表“ Kupac(客户)”和“ Proizvod(产品)”中有一些数据。 When I try to add something in this join table - "ProizvodRacun" I use this code: 当我尝试在此联接表中添加某些内容时-“ ProizvodRacun”,我使用以下代码:

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. 好的,我发现了问题,查询“ Kupac”时必须使用.Include()来包含其“ Racun”。 The "Racun" was always there just it wasn't loaded. “ Racun”始终在那里,只是没有加载。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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