简体   繁体   English

DataGridView中缺少一些多对一对象

[英]Some many to one objects missing in DataGridView

HI I have two Tables( Movie as Movy Entity and Producers as Producer ). 嗨,我有两个表(电影作为Movy实体和生产者作为生产者 )。 Movie has one producer and producer has many movies.here is the er diag Picture: ER Diagram 电影有一个制片人,制片人有很多电影。这是er diag 图片:ER图

When i set the data soure of my DataGridView to ctx.Movies.ToList() some movie in the list doesnt have any producer. 当我将DataGridView的数据源设置为ctx.Movies.ToList()时,列表中的某些电影没有任何生产者。 even though its not null in database. 即使它在数据库中不为null。 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(); } 

PS: [Picture:Database data] PS:[图片:数据库数据]

 /* 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. } 

You can try with Include method. 您可以尝试使用Include方法。

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

Alright I found the problem. 好吧,我发现了问题。 it is Entity Framework wasn't able to automap some string foreign key. 这是因为Entity Framework无法自动映射某些字符串外键。 for me those movie was having producerID = 'Universal' foreign key, that movies Producer Object was null(Couldn't map). 对我来说,那些电影的生产者ID ='Universal'外键,电影生产者对象为null(无法映射)。 so once I update my database like using following query follwoing: it working now: update Movies set Producer='Warner' where Producer='Universal'; 因此,一旦我更新数据库,就像使用以下查询一样:它现在可以正常工作: update Movies set Producer='Warner' where Producer='Universal';

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

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