EntityFrameworkCore和代码首先与Sqlite包括多个导航

时间:2017-12-01 17:05:23

标签: c# entity-framework .net-core entity-framework-core .net-standard

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

但我无法让数据库正常运行。 这是一个例子,我在打开edmx文件时看到的内容。

DesignerLayout

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
        }

我不知道为什么货币和观看时间总是为空。 我错了什么?

1 个答案:

答案 0 :(得分:0)

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

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