Exclusion of some tables from the Fluent Nhibernate Generation version

I have some existing asp.net membership and role tables in legacy db, and I map them to new objects with Fluent Nhibernate.

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

Can I tell Fluent Nhibernate to exclude certain tables from generation?

+3
source share
3 answers

SchemaAction.None() in your ClassMap.

+4
source

Another option is to create an attribute, say

public class DoNotAutoPersistAttribute : Attribute
{
}

AutoPersistenceModelGenerator Where AddEntityAssembly.

0

I succeeded with the 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
   ...
0
source

Source: https://habr.com/ru/post/1716432/


All Articles