0..1 - 1关系的问题 [英] Problem with 0..1 - 1 relation

查看:82
本文介绍了0..1 - 1关系的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始研究EF,我试图将两个数据库表映射到一个实体并面临挑战。
我使用学校数据库
我想从两个表中创建一个实体:课程和在线课程。我从OnlineCourse剪切了URL字段并将其粘贴到Course中。然后我将OnlineCourse表添加到课程表映射并删除了OnlineCourse。

我用combobox cbDepartment和datagridview dgvCourses制作了一个简单的表格。代码:


<前lang = x-aspx>私立SchoolEntities学校;

private void School_Load(object sender,EventArgs e)
{
school = new SchoolEntities();
ObjectQuery< Department>部门=学校。部门;

cbDepartment.DataSource = departments;
cbDepartment.DisplayMember =" Name";
}

private void cbDepartment_SelectedIndexChanged(object sender,EventArgs e)
{
Department department =(Department)cbDepartment.SelectedItem;

department.Course.Load(MergeOption.OverwriteChanges);

dgvCourses.DataSource = department.Course;

}




当我选择部门时,在课程表中没有字段与OnlineCourse中的字段相关联表,在我看到空数据网格视图。但是我希望看到所有课程记录但是有空URL列。我想这是因为Course和OnlineCourse表之间的0..1 - 1关系。
是什么解决了我的问题?
提前谢谢!

解决方案

嗨Viconia,

之所以如此您将获得一个空的数据网格视图,因为课程实体返回课程和OnlineCourse表之间的内部联接。

您可能需要考虑从课程派生出OnlineCourse,并为网格的URL列执行自定义绑定。

-Alex

I've started studying EF and I tried to map two database table to one entity and faced the challenge.
I uses school database
And I want to make one entity from two tables: Course and Online Course. I've cut URL field from OnlineCourse and pasted it into Course. Then I added OnlineCourse table to Course table mapping and deleted OnlineCourse.

And I made a simple Form with combobox cbDepartment and datagridview dgvCourses. The code:

private SchoolEntities school;

private void School_Load(object sender, EventArgs e)
{
    school = new SchoolEntities();
    ObjectQuery<Department> departments = school.Department;

     cbDepartment.DataSource = departments;
     cbDepartment.DisplayMember = "Name";
}

private void cbDepartment_SelectedIndexChanged(object sender, EventArgs e)
{
      Department department = (Department)cbDepartment.SelectedItem;
     
     department.Course.Load(MergeOption.OverwriteChanges);
			
    dgvCourses.DataSource = department.Course;

}




But when I select department, which have no fields in Course table associated with field in OnlineCourse table, in I see empty data grid view. But I want to see all course records but with empty URL column. I suppose that's happens because of 0..1 - 1 relation between Course and OnlineCourse tables.
Whats the solution of my problem?
 Thanks in advance!

解决方案

Hi Viconia,

The reason why you get an empty data grid view because Course entity returns a inner join between Course and OnlineCourse tables.

You might want to consider to make OnlineCourse derived from Course instead and do custom binding for the grid's URL column.

-Alex


这篇关于0..1 - 1关系的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆