DataGridView中缺少一些对象

时间:2016-09-30 14:08:59

标签: c# .net entity-framework

我有两张桌子(电影为 Movy 实体和生产者为制片人)。电影有一个制片人和制片人有很多电影。这是er diag Picture: ER Diagram

当我将我的DataGridView的数据源设置为ctx.Movies.ToList()时,列表中的某些电影没有任何制作人。即使它在数据库中不为空。 Picture:Program with missing producer

//Movie Entity Class
 public partial class Movy
    {
        public short VideoCode { get; set; }
        public string MovieTitle { get; set; }
        public string MovieType { get; set; }
        public string Rating { get; set; }
        public Nullable<float> RentalPrice { get; set; }
        public string ProducerID { get; set; }
        public string Director { get; set; }
        public string Media { get; set; }
        public Nullable<short> TotalStock { get; set; }
        public Nullable<short> NumberRented { get; set; }
        public virtual Producer Producer { get; set; }
    }
}

//Producer Entity Class
 public partial class Producer
    {
        public Producer()
        {
            this.Movies = new HashSet<Movy>();
        }
        public string ProducerID { get; set; }
        public string ProducerName { get; set; }
        public string CountryCode { get; set; }
        public virtual ICollection<Movy> Movies { get; set; }
    }

// MyDBEntities ctx
public partial class MYDBEntities : DbContext
    {
        public MYDBEntities()
            : base("name=MYDBEntities")
        {
        }
        public virtual DbSet<Movy> Movies { get; set; }
        public virtual DbSet<Producer> Producers { get; set; }
    }

//this is my function in windows form which is having some producer missing in movie datalist. i have added the picture of output

     private void Form1_Load(object sender, EventArgs e)
        {
            MYDBEntities ct = new MYDBEntities();
            dataGridView1.DataSource = ct.Movies.ToList();
        }

//Movie Entity Class public partial class Movy { public short VideoCode { get; set; } public string MovieTitle { get; set; } public string MovieType { get; set; } public string Rating { get; set; } public Nullable<float> RentalPrice { get; set; } public string ProducerID { get; set; } public string Director { get; set; } public string Media { get; set; } public Nullable<short> TotalStock { get; set; } public Nullable<short> NumberRented { get; set; } public virtual Producer Producer { get; set; } } } //Producer Entity Class public partial class Producer { public Producer() { this.Movies = new HashSet<Movy>(); } public string ProducerID { get; set; } public string ProducerName { get; set; } public string CountryCode { get; set; } public virtual ICollection<Movy> Movies { get; set; } } // MyDBEntities ctx public partial class MYDBEntities : DbContext { public MYDBEntities() : base("name=MYDBEntities") { } public virtual DbSet<Movy> Movies { get; set; } public virtual DbSet<Producer> Producers { get; set; } } //this is my function in windows form which is having some producer missing in movie datalist. i have added the picture of output private void Form1_Load(object sender, EventArgs e) { MYDBEntities ct = new MYDBEntities(); dataGridView1.DataSource = ct.Movies.ToList(); }

P.S:[图片:数据库数据]

/* i checked each product and its producer. some producer is null there.
  even though its not null in database(I have uploaded db pic) [3]*/

            List<Movy> ls =  ct.Movies.ToList();
            foreach(Movy mov in ls)
            {
                //some mov.Producer is null here. 
            }

2 个答案:

答案 0 :(得分:0)

您可以尝试使用Include方法。

MYDBEntities ct = new MYDBEntities();
dataGridView1.DataSource = ct.Movies.Include("Producer").ToList();

答案 1 :(得分:0)

好吧我发现了这个问题。它是Entity Framework无法自动化一些字符串外键。 对我来说,这部电影有制作人ID =&#39;环球&#39;外键,电影制片人对象为空(无法映射)。所以一旦我更新我的数据库就像使用以下查询一样:它现在正在工作: update Movies set Producer='Warner' where Producer='Universal';