EntityFrameworkCore 和 code first with Sqlite,包括多个导航

EntityFrameworkCore and code first with Sqlite including multiple navigation

回到 vs2015,我使用外部程序创建/修改我的数据库,并使用 ef 设计器在 vs 中更新它。 vs2017 也可以将设计器与某些第三方工具一起使用,但我想先切换到代码。在这种情况下,我想将我的 class 库切换到 .net 标准。

但是我无法让我的数据库正常工作。 这是一个示例,当我打开 edmx 文件时看到的内容。

ID不递增,由外部服务提供。该服务的每个用户都有一个唯一的 ID,我将其存储在那里。


现在我的(非常简单的)代码优先模型:

    public class Users {
         public int UserID { get; set; }
         public string Name { get; set; }

         public Currency Currency { get; set; }
         public TimeWatched TimeWatched { get; set; }
    }

    public class Currency {
        public int UserID { get; set; }
        public int CurrencyValue { get; set; }

        public Users Users { get; set; }
    }

    public class TimeWatched {
        public int UserID { get; set; }
        public long Time { get; set; }

        public Users Users { get; set; }
    }

我的 DbContext 看起来像这样:

     using Microsoft.Data.Sqlite;
     using Microsoft.EntityFrameworkCore;

    public class context : DbContext {

    public DbSet<Users> Users { get; set; }
    public DbSet<Currency> Currency { get; set; }
    public DbSet<TimeWatched> TimeWatched { get; set; }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) {
        var connectionStringBuilder = new SqliteConnectionStringBuilder { DataSource = "test.db" };
        var connectionString = connectionStringBuilder.ToString();
        var connection = new SqliteConnection(connectionString);

        optionsBuilder.UseSqlite(connection);
    }

    protected override void OnModelCreating(ModelBuilder modelBuilder) {
        base.OnModelCreating(modelBuilder);

        modelBuilder.Entity<Users>()
            .HasKey(c => c.UserID);

        modelBuilder.Entity<Users>()
            .Property(c => c.UserID)
            .ValueGeneratedNever();

        modelBuilder.Entity<Users>()
            .HasOne(a => a.Currency)
            .WithOne(b => b.Users)
            .HasForeignKey<Currency>(b => b.UserID);

        modelBuilder.Entity<Users>()
            .HasOne(a => a.TimeWatched)
            .WithOne(b => b.Users)
            .HasForeignKey<TimeWatched>(b => b.UserID);

        modelBuilder.Entity<Currency>()
            .HasKey(k => k.UserID);

        modelBuilder.Entity<TimeWatched>()
            .HasKey(k => k.UserID);
    }
}

它是这样测试的:

            using(var context = new testStandard.context()) {

            context.Database.EnsureCreated();

            var user = new Users {
                Name = "test",
                TwitchUser = 123456,
                Currency = new Currency {
                    TwitchUser = 123456,
                    CurrencyValue = 999
                },
                TimeWatched = new TimeWatched {
                    TwitchUser = 123456,
                    Time = 500
                }
            };

            context.Users.Add(user);

            context.SaveChanges();
        }

        using(var context = new context()) {
            var user = context.Users.First();
            // Currency and TimeWatched are always null
        }

我不知道为什么 currency 和 timewatched 总是空的。 我做错了什么?

谢谢杰米! 我找到了解决方案:

using(var context = new context()) {
var user = context.Users
            .Include(c => c.Currency)
            .Include(tw => tw.TimeWatched)
            .First();
}