我正在学习EF Code First,但我遇到了问题。 我有主要和详细的抽象类,在数据绑定到UI控件期间,我收到一个异常。
在在线课程的WPF主/详细信息表格中,我为学生提供了一个DataGrid(详细信息)。
当我尝试向DataGrid
添加一行时抛出此异常
无法创建NameSpace.student的实例,因为它是一个抽象类。
那么如何将Students
集合映射为OnlineStudents
在onlineCourse和OfflineStudents
在offlineCourse中?
public abstract class Course
{
public int Id { get; set; }
public string Description{ get; set; }
public System.DateTime StartDate { get; set; }
public virtual System.Collections.ObjectModel.ObservableCollection<Student> Students { get; set; }
}
public abstract class Student
{
public int Id { get; set; }
public string Name { get; set; }
public int CourseID { get; set; }
public virtual Course course { get; set; }
}
public class OnlineCouse : Course
{
public string URI { get; set; }
}
public class OnlineStudent : Student
{
public string Email { get; set; }
}
class InstContext : DbContext
{
public InstContext()
: base()
{
}
public DbSet<Course> Course{ get; set; }
public DbSet<Student> Student{ get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
Database.SetInitializer<InstContext>(new DropCreateDatabaseIfModelChanges<InstContext>());
modelBuilder.Entity<OnlineCouse>().Map(m =>
{
m.MapInheritedProperties();
m.ToTable("OnlineCourse");
});
modelBuilder.Entity<OnlineStudent>().Map(m =>
{
m.MapInheritedProperties();
m.ToTable("OnlineStudent");
});
}
}
以下是Xaml
<Window.Resources>
<CollectionViewSource x:Key="onlineCourseViewSource" d:DesignSource="{d:DesignInstance {x:Type XData:OnlineCourse}, CreateList=True}"/>
<CollectionViewSource x:Key="onlineCourseStudentsViewSource" Source="{Binding Students, Source={StaticResource gSVoucherViewSource}}"/>
</Window.Resources>
<Grid DataContext="{StaticResource onlineCourseViewSource}">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Label Content="Id:" Grid.Row="0" Grid.Column="0" HorizontalAlignment="Left" Margin="3" VerticalAlignment="Center"/>
<TextBox x:Name="idTextBox" Grid.Row="0" Grid.Column="1" HorizontalAlignment="Left" Height="23" Margin="3" Text="{Binding Id, Mode=TwoWay, NotifyOnValidationError=true, ValidatesOnExceptions=true}" VerticalAlignment="Center" Width="120"/>
<Label Content="Email:" Grid.Row="1" Grid.Column="0" HorizontalAlignment="Left" Margin="3" VerticalAlignment="Center"/>
<TextBox x:Name="EmailTextBox" Grid.Row="1" Grid.Column="1" HorizontalAlignment="Left" Height="23" Margin="3" Text="{Binding Email, Mode=TwoWay, NotifyOnValidationError=true, ValidatesOnExceptions=true}" VerticalAlignment="Center" Width="120"/>
<DataGrid x:Name="studentsDataGrid" Grid.Row="2" Grid.Column="0" ItemsSource="{Binding Source={StaticResource onlineCourseStudentsViewSource}}" Grid.ColumnSpan="2" AutoGenerateColumns="False" EnableRowVirtualization="True" RowDetailsVisibilityMode="VisibleWhenSelected">
<DataGrid.Columns>
<DataGridTextColumn x:Name="idColumn" Binding="{Binding Id}" Header="Id" Width="SizeToHeader"/>
<DataGridTextColumn x:Name="nameColumn" Binding="{Binding Name}" Header="In Area" IsReadOnly="True" Width="SizeToHeader"/>
</DataGrid.Columns>
</DataGrid>
</Grid>
问题是: 如果我有一个抽象的主类,它有一个抽象的子类集合,如何在派生的主类中重写它以获得派生的子集合?