mercredi 4 mai 2016

How to override Entity Framework fluent mappings?

I'm trying to create a new and dedicated Entity Framework DbContext for Integration Testing with in-memory SQLite database.Below is the OnModelCreating method:

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Configurations.AddFromAssembly(typeof(XXX).Assembly);
        var sqliteConnectionInitializer = new SqliteCreateDatabaseIfNotExists<YYY>(modelBuilder);
        Database.SetInitializer(sqliteConnectionInitializer);

        base.OnModelCreating(modelBuilder);
    }

As you can see I'm adding my usual fluent mappings - the same that I'm using for my "usual" context. The problem is that SQLite is a little different than SQLServer, so the mapping doesn't work i.e. I can't map this:

Property(x => x.Timestamp).HasColumnOrder(11).IsRequired().IsRowVersion().HasColumnName("timestamp");

Timestamps are not supported in SQLite. The question is: how can I override the fluent mapping i.e. by convention - in this case it's enough to get rid of the IsRowVersion() - it's totally OK not to have optimistic concurrency in integration tests.

Aucun commentaire:

Enregistrer un commentaire