简体   繁体   English

首先在WPF MVVM实体框架中初始化数据库的文件

[英]Which file to Initialize Database in WPF MVVM Entity Frameworks Code First

WFP MVVM EF代码优先架构

I've got about six months experience with WPF/MVVM and am trying to incorporate EF Code First in my project. 我在WPF / MVVM方面拥有大约六个月的经验,并且正在尝试将EF Code First纳入我的项目。 Two questions: 两个问题:

  1. Is the layout in the image above the best to use? 图像上方的布局是否最适合使用? Is it better to use sub-projects or would sub-folders work just as well? 使用子项目会更好还是使用子文件夹? I'm open to suggestions. 我愿意提出建议。

  2. Got this code to work in MainWindow.xaml.cs : 得到此代码以在MainWindow.xaml.cs工作:

     namespace someNamespace { /// <summary> /// Interaction logic for MainWindow.xaml /// </summary> public partial class MainWindow : Window { private EmployeeDBContext context = new EmployeeDBContext(); public MainWindow() { InitializeComponent(); Database.SetInitializer(new DropCreateDatabaseIfModelChanges<EmployeeDBContext>()); } private void Window_Loaded(object sender, RoutedEventArgs e) { System.Windows.Data.CollectionViewSource employeeViewSource = ((System.Windows.Data.CollectionViewSource)(this.FindResource("employeeViewSource"))); context.Employees.Load(); employeeViewSource.Source = context.Employees.Local; } private void Button_Search(object sender, RoutedEventArgs e) { //event handler code using (var context = new EmployeeDBContext()) { context.Employees.Add(employee); context.SaveChanges(); } } protected override void OnClosing(System.ComponentModel.CancelEventArgs e) { base.OnClosing(e); this.context.Dispose(); } private void Button_Exit(object sender, RoutedEventArgs e) { this.Close(); } } } 
  1. Sub-folders or projects makes little difference. 子文件夹或项目没有什么区别。 If you have separate projects you can build them one at a time - a bit faster. 如果您有单独的项目,则可以一次构建一个项目-快一点。

    2. I'm pretty new to MVVM too but I do know that you should keep code-behind to a minimum. 2.我对MVVM也很陌生,但我确实知道您应该将代码隐藏量保持在最低水平。 What you wrote there should be done using RelayCommands. 您在此处编写的内容应该使用RelayCommands完成。 Also you wouldn't call the dbContext from the ViewModel. 同样,您不会从ViewModel调用dbContext。 The ViewModel talks to a Dataservice which manages the dbContext. ViewModel与管理dbContext的Dataservice对话。

    Not sure what the title means. 不知道标题是什么意思。 Are you asking where to put your ADONet Entity Data Model? 您是否在问将ADONet实体数据模型放在哪里?

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

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