Skip to content
On this page

Database Storage

For each top level document type, Marten will generate database objects for:

  • A database table called mt_doc_[document alias], where the document alias is typically derived from the class name of the top level document type
  • A function called mt_upsert_[document alias]
  • A function called mt_update_[document alias]
  • A function called mt_insert_[document alias]
  • A function called mt_overwrite_[document alias], an upsert function that bypasses any kind of configured optimistic concurrency checks

Overriding the Database Schema

By default, all of the document type tables will be created and used from the public schema. That can be overridden globally with this usage:

cs
var store = DocumentStore.For(opts =>
{
    opts.Connection("some connection string");
    opts.DatabaseSchemaName = "other";
});

snippet source | anchor

If you choose, you can override the default database schema name for the DocumentStore by explicitly setting the schema for an individual document type through the MartenRegistry fluent interface like this:

cs
var store = DocumentStore.For(opts =>
{
    opts.Connection("some connection string");
    opts.DatabaseSchemaName = "other";

    // This would take precedence for the
    // User document type storage
    opts.Schema.For<User>()
        .DatabaseSchemaName("users");
});

snippet source | anchor

Or by using an attribute on your document type:

cs
[DatabaseSchemaName("organization")]
public class Customer
{
    [Identity] public string Name { get; set; }
}

snippet source | anchor

Type Aliases

In the not unlikely case that you need to disambiguate table storage for two or more documents with the same type name, you can override the type alias either programmatically with MartenRegistry:

cs
var store = DocumentStore.For(_ =>
{
    _.Connection(ConnectionSource.ConnectionString);

    _.Schema.For<User>().DocumentAlias("folks");
});

snippet source | anchor

or by decorating the actual document class with an attribute:

cs
[DocumentAlias("johndeere")]
public class Tractor
{
    public string id;
}

snippet source | anchor

Released under the MIT License.