4
votes

I have some existing asp.net membership and roles tables in a legacy db and I am mapping them to new entities with Fluent Nhibernate.

I also generate the schema directly from Fluent Nhibernate and I then manually tweak the generated sql script to exclude the existing tables.

Is it possible to say to Fluent Nhibernate to exclude from generation certain tables?

3

3 Answers

6
votes

SchemaAction.None() in your ClassMap.

0
votes

Another option would be to create an attribute, say

public class DoNotAutoPersistAttribute : Attribute
{
}

Then in AutoPersistenceModelGenerator you could check for this attribute in the Where clause of AddEntityAssembly.

0
votes

I've managed this with an attribute + convention:

 public enum SchemaAction
  {
    None
  }


 [Serializable]
 [AttributeUsage(AttributeTargets.Class)]
 public class SchemaActionAttribute : Attribute
 {
    private readonly SchemaAction schemaAction = SchemaAction.None;

    public SchemaActionAttribute()
    {
    }

    public SchemaActionAttribute(SchemaAction schemaAction)
    {
      this.schemaAction = schemaAction;
    }

    public SchemaAction GetSchemaAction()
    {
      return schemaAction;
    }
  }

  /// <summary>
  ///  overrides the default action for entities when creating/updating the schema 
  ///  based on the class having a Schema attribute (<see cref="SchemaActionAttribute" />)
  /// </summary>
  public class SchemaActionConvention : IClassConvention
  {
    public void Apply(IClassInstance instance)
    {

      object[] attributes = instance.EntityType.GetCustomAttributes(true);
      foreach (object t in attributes)
      {
        if (t is SchemaActionAttribute)
        {
          var a = (SchemaActionAttribute) t;
          switch(a.GetSchemaAction())
          {
            case SchemaAction.None: 
              instance.SchemaAction.None();
              return;
            default: throw new ApplicationException("That schema action:" + a.GetSchemaAction().ToString() + " is not currently implemented.");
          }

        }
      }
    }
  }

...

 [SchemaAction(SchemaAction.None)]
 public class TextItem : Entity
   ...