EntityFrameworkCore and code first with Sqlite including multiple navigation

.net-core .net-standard c# entity-framework entity-framework-core

Question

back in vs2015 i used an external program to create / modify my database and update it in vs with the ef designer. It is also possible with vs2017 to use the designer with some third party tools, but i want to switch to code first. in this context i want to switch my class library to .net standard.

But i have trouble to get my databases working. Here is an example, what i see when i open the edmx file.

DesignerLayout

The ID is not incremented, it is provided by an external service. every user of the service has an unique id and i store it there.


And now my (very simple) code first models:

    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; }
    }

My DbContext looks like this:

     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);
    }
}

It test it like this:

            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
        }

I dont know why currency and timewatched are always null. what do i wrong?

1
0
12/1/2017 7:14:49 PM

Accepted Answer

Thanks Jamie! I found a solution:

using(var context = new context()) {
var user = context.Users
            .Include(c => c.Currency)
            .Include(tw => tw.TimeWatched)
            .First();
}
0
12/5/2017 1:02:57 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow