我在遗留数据库中有一些现有的asp.net成员资格和角色表,我正在使用Fluent Nhibernate将它们映射到新实体。
我也直接从Fluent Nhibernate生成模式,然后手动调整生成的sql脚本以排除现有表。
是否有可能对Fluent Nhibernate说出从某代表中排除?
答案 0 :(得分:5)
SchemaAction.None()
。
答案 1 :(得分:0)
另一种选择是创建一个属性,比如
public class DoNotAutoPersistAttribute : Attribute
{
}
然后在AutoPersistenceModelGenerator中,您可以在AddEntityAssembly的Where子句中检查此属性。
答案 2 :(得分:0)
我使用属性+约定来管理它:
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
...