使用 EntityFrameworkCore 查看模型

View Model with EntityFrameworkCore

我开始自学 .net-core 和 Avalonia UI。

我启动了 Avalonia UI 教程,它按预期运行。 http://www.avaloniaui.net/docs/tutorial/creating-model-viewmodel

但是大多数时候,您可以从教程中复制代码并且它可以工作,但是您不理解...

现在我想,可以将 "fake database" 更改为真实数据库。所以我开始包括 Microsoft.EmtityFrameworkCore。数据库存在,包含在内并且编译并运行。

namespace Decksumme.Models
{
    public class ParticipantsContext : DbContext
    {
        public DbSet<Participant> Participants { get; set; }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            optionsBuilder.UseSqlite("Data Source=decksumme.db");
        }
    }
}

我更新了 ListViewModel

namespace Decksumme.ViewModels
{
    public class DecksummeListViewModel : ViewModelBase
    {
        public DecksummeListViewModel(DbSet<Participant> participants)
        {
            Participants = new ObservableCollection<DbSet<Participant>>();
        }

        public ObservableCollection<DbSet<Participant>> Participants { get; }
    }
}

编辑视图模型得到增强并编译无误

namespace Decksumme.ViewModels
{
    class EditParticipantViewModel : ViewModelBase
    {
        string forename;
        string name;
        string discipline;

        public EditParticipantViewModel()
        {
            var okEnabled = this.WhenAnyValue(
                x => x.forename,
                x => !string.IsNullOrWhiteSpace(x)
            );

            Ok = ReactiveCommand.Create(
                () => new Participant
                {
                    Forename = Forename,
                    Name = Name,
                    Discipline = Discipline,
                },
                okEnabled
            );
            Cancel = ReactiveCommand.Create(() => { });
        }
        public string Forename { 
            get => forename; 
            set =>  this.RaiseAndSetIfChanged(ref forename, value); 
        }
        public string Name { 
            get => name; 
            set => this.RaiseAndSetIfChanged(ref name, value);
        }
        public string Discipline { 
            get => discipline; 
            set => this.RaiseAndSetIfChanged(ref discipline, value);
        }
        public string Results { get; set; }

        public ReactiveCommand<Unit, Participant> Ok { get; }
        public ReactiveCommand<Unit, Unit> Cancel { get; }
    }
}

现在我失去了一点,MainWondowViewModel。

namespace Decksumme.ViewModels
{
    public class MainWindowViewModel : ViewModelBase
    {
        ViewModelBase content;

        public MainWindowViewModel(ParticipantsContext db)
        {
            Content = List = new DecksummeListViewModel(db.Participants);
        }

        public ViewModelBase Content
        {
            get => content;
            private set => this.RaiseAndSetIfChanged(ref content, value);
        }

        public DecksummeListViewModel List { get; }

        public void AddParticipant()
        {
            var vm = new EditParticipantViewModel();

            Observable.Merge(
                vm.Ok,
                vm.Cancel.Select(_ => (Participant)null))
                .Take(1)
                .Subscribe(model =>
                {
                    if(model != null)
                    {
                        List.Participants.Add(model);
                    }

                    Content = List;
                });

            Content = vm;
        }
    }
}

在 AddParticipant 方法中。 List.Participant.add(型号);给我错误

ViewModels\MainWindowViewModel.cs(39,47): error CS1503: Argument "1": Konvertierung von "Decksumme.Models.Participant" in "Microsoft.EntityFrameworkCore.DbSet<Decksumme.Models.Participant>" nicht möglich.

现在是知识不足的问题。我对 Observable 的理解有误吗?我用错了数据库吗?还是我必须在某个时候进行转换?

public ObservableCollection<DbSet<Participant>> Participants { get; }

您正在创建数据集集合而不是 Participant 对象集合。您可能需要 ObservableCollection<Participant>

天哪,@kekekeks 是对的。这是构建错误的解决方案。

namespace Decksumme.ViewModels
{
    public class DecksummeListViewModel : ViewModelBase
    {
        public DecksummeListViewModel(DbSet<Participant> participants)
        {
            Participants = new ObservableCollection<Participant>(participants);
        }

        public ObservableCollection<Participant> Participants { get; }
    }
}

需要测试。