public InheritanceSqliteFixture()
        {
            _serviceProvider
                = new ServiceCollection()
                  .AddEntityFramework()
                  .AddSqlite()
                  .ServiceCollection()
                  .AddSingleton(TestSqliteModelSource.GetFactory(OnModelCreating))
                  .AddSingleton <ILoggerFactory>(new TestSqlLoggerFactory())
                  .BuildServiceProvider();

            _testStore = SqliteTestStore.CreateScratch();

            var optionsBuilder = new DbContextOptionsBuilder();

            optionsBuilder.UseSqlite(_testStore.Connection);
            _options = optionsBuilder.Options;

            // TODO: Do this via migrations & update pipeline

            _testStore.ExecuteNonQuery(@"
                DROP TABLE IF EXISTS Country;
                DROP TABLE IF EXISTS Animal;

                CREATE TABLE Country (
                    Id int NOT NULL PRIMARY KEY,
                    Name nvarchar(100) NOT NULL
                );

                CREATE TABLE Animal (
                    Species nvarchar(100) NOT NULL PRIMARY KEY,
                    Name nvarchar(100) NOT NULL,
                    CountryId int NOT NULL ,
                    IsFlightless bit NOT NULL,
                    EagleId nvarchar(100),
                    'Group' int,
                    FoundOn tinyint,
                    Discriminator nvarchar(255),

                    FOREIGN KEY(countryId) REFERENCES Country(Id),
                    FOREIGN KEY(EagleId) REFERENCES Animal(Species)
                );

                CREATE TABLE Plant (
                    Genus int NOT NULL,
                    Species nvarchar(100) NOT NULL PRIMARY KEY,
                    Name nvarchar(100) NOT NULL,
                    CountryId int,
                    HasThorns bit,

                    FOREIGN KEY(countryId) REFERENCES Country(Id)
                );
            ");

            using (var context = CreateContext())
            {
                SeedData(context);
            }
            TestSqlLoggerFactory.Reset();
        }
        public InheritanceSqliteFixture()
        {
            _testStore = SqliteTestStore.CreateScratch();

            _options = new DbContextOptionsBuilder()
                .UseSqlite(_testStore.Connection)
                .UseInternalServiceProvider(new ServiceCollection()
                    .AddEntityFrameworkSqlite()
                    .AddSingleton(TestSqliteModelSource.GetFactory(OnModelCreating))
                    .AddSingleton<ILoggerFactory>(new TestSqlLoggerFactory())
                    .BuildServiceProvider())
                .Options;

            // TODO: Do this via migrations & update pipeline

            _testStore.ExecuteNonQuery(@"
                DROP TABLE IF EXISTS Country;
                DROP TABLE IF EXISTS Animal;

                CREATE TABLE Country (
                    Id int NOT NULL PRIMARY KEY,
                    Name nvarchar(100) NOT NULL
                );

                CREATE TABLE Animal (
                    Species nvarchar(100) NOT NULL PRIMARY KEY,
                    Name nvarchar(100) NOT NULL,
                    CountryId int NOT NULL ,
                    IsFlightless bit NOT NULL,
                    EagleId nvarchar(100),
                    'Group' int,
                    FoundOn tinyint,
                    Discriminator nvarchar(255),

                    FOREIGN KEY(countryId) REFERENCES Country(Id),
                    FOREIGN KEY(EagleId) REFERENCES Animal(Species)
                );

                CREATE TABLE Plant (
                    Genus int NOT NULL,
                    Species nvarchar(100) NOT NULL PRIMARY KEY,
                    Name nvarchar(100) NOT NULL,
                    CountryId int,
                    HasThorns bit,

                    FOREIGN KEY(countryId) REFERENCES Country(Id)
                );
            ");

            using (var context = CreateContext())
            {
                SeedData(context);
            }

            TestSqlLoggerFactory.Reset();
        }
Ejemplo n.º 3
0
        public void It_allows_foreign_key_to_unique_index()
        {
            var builder       = new DbContextOptionsBuilder();
            var sqliteBuilder = builder.UseSqlite(_testStore.ConnectionString);
            var options       = builder.Options;

            _testStore.ExecuteNonQuery(@"
CREATE TABLE User (
    Id INTEGER PRIMARY KEY,
    AltId INTEGER NOT NULL UNIQUE
);
CREATE TABLE Comment (
    Id INTEGER PRIMARY KEY,
    UserAltId INTEGER NOT NULL,
    Comment TEXT,
    FOREIGN KEY (UserAltId) REFERENCES User (AltId)
);");

            long id;

            using (var context = new BloggingContext(options))
            {
                var entry = context.User.Add(new User {
                    AltId = 1356524
                });
                context.Comments.Add(new Comment {
                    User = entry.Entity
                });
                context.SaveChanges();
                id = entry.Entity.Id;
            }

            using (var context = new BloggingContext(options))
            {
                var comment = context.Comments.Include(u => u.User).Single();
                Assert.Equal(id, comment.User.Id);
            }
        }