Fluent Nhibernate - how do i specify table schemas when auto generating tables in SQL CE 4

Posted by daffers on Stack Overflow See other posts from Stack Overflow or by daffers
Published on 2011-11-16T09:49:08Z Indexed on 2011/11/16 9:50 UTC
Read the original article Hit count: 233

I am using SQL CE as a database for running local and CI integration tests (normally our site runs on normal SQL server). We are using Fluent Nhibernate for our mapping and having it create our schema from our Mapclasses. There are only two classes with a one to many relationship between them. In our real database we use a non dbo schema. The code would not work with this real database at first until i added schema names to the Table() methods. However doing this broke the unit tests with the error...

System.Data.SqlServerCe.SqlCeException : There was an error parsing the query. [ Token line number = 1,Token line offset = 26,Token in error = User ]

These are the classes and associatad MapClasses (simplified of course)

    public class AffiliateApplicationRecord
{
    public virtual int Id { get; private set; }
    public virtual string CompanyName { get; set; }
    public virtual UserRecord KeyContact { get; private set; }

    public AffiliateApplicationRecord()
    {
        DateReceived = DateTime.Now;
    }

    public virtual void AddKeyContact(UserRecord keyContactUser)
    {
        keyContactUser.Affilates.Add(this);
        KeyContact = keyContactUser;
    }
}

public class AffiliateApplicationRecordMap : ClassMap<AffiliateApplicationRecord>
{
    public AffiliateApplicationRecordMap()
    {
        Schema("myschema");
        Table("Partner");
        Id(x => x.Id).GeneratedBy.Identity();
        Map(x => x.CompanyName, "Name");
                    References(x => x.KeyContact)
            .Cascade.All()
            .LazyLoad(Laziness.False)
            .Column("UserID");       
    }
}

public class UserRecord
{
    public UserRecord()
    {
        Affilates = new List<AffiliateApplicationRecord>();
    }

    public virtual int Id { get; private set; }
    public virtual string Forename { get; set; }
    public virtual IList<AffiliateApplicationRecord> Affilates { get; set; }
}

public class UserRecordMap : ClassMap<UserRecord>
{
    public UserRecordMap()
    {
        Schema("myschema");
        Table("[User]");//Square brackets required as user is a reserved word
        Id(x => x.Id).GeneratedBy.Identity();
        Map(x => x.Forename);

        HasMany(x => x.Affilates);

    }
}

And here is the fluent configuraton i am using ....

      public static ISessionFactory CreateSessionFactory()
    {
        return Fluently.Configure()
            .Database(
                MsSqlCeConfiguration.Standard
                    .Dialect<MsSqlCe40Dialect>()
                    .ConnectionString(ConnectionString)
                    .DefaultSchema("myschema"))
            .Mappings(m => m.FluentMappings.AddFromAssembly(typeof(AffiliateApplicationRecord).Assembly))
            .ExposeConfiguration(config => new SchemaExport(config).Create(false, true))
            .ExposeConfiguration(x => x.SetProperty("connection.release_mode", "on_close")) //This is included to deal with a SQLCE issue http://stackoverflow.com/questions/2361730/assertionfailure-null-identifier-fluentnh-sqlserverce
            .BuildSessionFactory();

    }

The documentation on this aspect of fluent is pretty weak so any help would be appreciated

© Stack Overflow or respective owner

Related posts about nhibernate

Related posts about fluent-nhibernate