0

我有两个实体:(实体框架代码优先方法)

public class Work
{
    public Guid Id { get; set; }
    public string Title { get; set; }
    public virtual Department Department { get; set; }
    public virtual Guid DepartmentId { get; set; }
}

public class Department
{
    public Department()
    {
        Works = new ObservableCollection<Work>();
    }

    public Guid Id { get; set; }
    public string Name { get; set; }
    public virtual ICollection<Work> Works { get; private set; }
}

我有以下 XAML:(DataGrid 工作项)
(我想显示一个DataGridComboBoxColumn包含所有部门名称的)

<DataGrid.Columns>
                                <DataGridTextColumn x:Name="idColumn5" Binding="{Binding Id}" Header="Id" Width="100"/>
                                <DataGridTextColumn x:Name="titleColumn2" Binding="{Binding Title}" Header="Title" Width="250"/>   

                                <DataGridComboBoxColumn SelectedValueBinding="{Binding Path=Department.Name, Mode=TwoWay, 
                                    NotifyOnTargetUpdated=True}"  Header="Department Name"
                                                   ItemsSource="{x:Static my:MainWindow.Departments}"
                                                        DisplayMemberPath="Name" SelectedValuePath="ID"
                                                    Width="140"/>

                                <DataGridTextColumn x:Name="priceColumn2" Binding="{Binding Price}" Header="Price" Width="90"/>
                            </DataGrid.Columns>

VS 2012 概述(错误)ItemsSource="{x:Static my:MainWindow.Departments}"

Invalid object name 'dbo.Departments'.

但我可以开始这个项目。

当我尝试添加新工作时,我得到(内部异常):

INSERT 语句与 FOREIGN KEY 约束“FK_dbo.Works_dbo.Departments_DepartmentId”冲突。冲突发生在数据库“MyDbName”、表“dbo.Departments”、列“id”中。

我在 MainWindow 对象中也有这个属性:

    public static ICollection<Department> Departments
    {
        get
        {
            if (System.Diagnostics.Process.GetCurrentProcess().ProcessName == "devenv")
                return new List<Department>();
            return _context.Departments.ToList();
        }
    }

我是 WPF 和这种类型的数据绑定和 XAML 的新手。
将感谢任何可以帮助解决问题的答案。

4

1 回答 1

0

只需Binding Path=要从更改Department.NameDepartmentId

于 2013-06-07T16:01:31.210 回答