我的问题是我试图用数据种子实体框架核心数据库,在我看来下面的代码显示工作.我已经意识到这不应该在ApplicationDbContext构造函数中调用,应该从中调用,startup但我不知道如何做到这一点.
编辑:根据Ketrex提供的解决方案,我的解决方案如下:
Startup.cs:
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
...
app.ApplicationServices.GetRequiredService<ApplicationDbContext>().Seed();
}
Run Code Online (Sandbox Code Playgroud)
种子延伸:
public static class DbContextExtensions
{
public static void Seed(this ApplicationDbContext context)
{
// Perform database delete and create
context.Database.EnsureDeleted();
context.Database.EnsureCreated();
// Perform seed operations
AddCountries(context);
AddAreas(context);
AddGrades(context);
AddCrags(context);
AddClimbs(context);
// Save changes and release resources
context.SaveChanges();
context.Dispose();
}
private static void AddCountries(ApplicationDbContext context)
{
context.AddRange(
new Country { Name = "England", Code = "En" },
new Country { Name = …Run Code Online (Sandbox Code Playgroud) 我有一个带有项目控件的用户控件,我希望它具有 2 种完全不同的项目模板样式,具体取决于交替索引。我看过很多关于如何根据索引更改背景颜色但不更改每个索引的样式的教程。这是我到目前为止所拥有的。
定义的模板:
<UserControl.Resources>
<DataTemplate x:Key="ItemLeft" >
<Border Background="Blue" Height="10">
<!-- Define Left Style -->
</Border>
</DataTemplate>
<DataTemplate x:Key="ItemRight">
<Border Background="Red" Height="10">
<!-- Define Right Style -->
</Border>
</DataTemplate>
</UserControl.Resources>
Run Code Online (Sandbox Code Playgroud)
我删除了数据模板代码以使其更易于阅读。它不仅仅是边框颜色。
物品控制:
<ItemsControl Name="ItemControl" AlternationCount="2">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<VirtualizingStackPanel IsItemsHost="True"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.Style>
<Style>
<Style.Triggers>
<Trigger Property="ItemsControl.AlternationIndex" Value="0">
<Setter Property="ItemsControl.ItemTemplate" Value="{StaticResource ItemRight}"/>
</Trigger>
<Trigger Property="ItemsControl.AlternationIndex" Value="1">
<Setter Property="ItemsControl.ItemTemplate" Value="{StaticResource ItemLeft}"/>
</Trigger>
</Style.Triggers>
</Style>
</ItemsControl.Style>
</ItemsControl>
Run Code Online (Sandbox Code Playgroud)
我很确定我不应该在这种风格中做这种类型的触发器,但我不知道还能怎么做。我是使用 WPF 的新手,我发现它的大部分内容都非常直观,但我在这里迷路了。我想尝试将它包含在 XAML 代码中。
谢谢