如果架构名称中包含点,如何导出 SQLite 的 NHibernate 架构?

发布于 2024-11-29 04:28:22 字数 557 浏览 2 评论 0原文

我正在尝试使用 Fluent NHibernate 设置 SQLite 进行单元测试 如此处所示,但表名称未按我的预期生成。

有些表的模式里面有点,这似乎破坏了生成。 (Dots 与我的生产环境中的 Microsoft SQL Server 配合得很好。)

示例:

[Foo.Bar.Schema].[TableName]

结果:

TestFixture failed: System.Data.SQLite.SQLiteException : SQLite error
unknown database Foo

如何指示 SQLite 将点转换为下划线或其他内容,以便我可以运行单元测试?

(我尝试向架构名称添加括号但没有成功)

I'm trying to set up SQLite for unit testing with Fluent NHibernate as shown here but the table names isn't being generated as I expected.

Some tables have schemas with dots inside which seems to break the generation. (Dots works perfectly well with Microsoft SQL Server which I have in my production environment.)

Example:

[Foo.Bar.Schema].[TableName]

Result:

TestFixture failed: System.Data.SQLite.SQLiteException : SQLite error
unknown database Foo

How do I instruct SQLite to translate the dots to underscores or something so I can run my unit tests?

(I've tried adding brackets to the schema names with no success)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

染墨丶若流云 2024-12-06 04:28:22

您可以使用约定
http://wiki. Fluentnhibernate.org/Conventions

*更新

public static class PrivatePropertyHelper
    {
        // from http://stackoverflow.com/questions/1565734/is-it-possible-to-set-private-property-via-reflection
        public static T GetPrivatePropertyValue<T>(this object obj, string propName)
        {
            if (obj == null) throw new ArgumentNullException("obj");
            PropertyInfo pi = obj.GetType().GetProperty(propName, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
            if (pi == null) throw new ArgumentOutOfRangeException("propName", string.Format("Property {0} was not found in Type {1}", propName, obj.GetType().FullName));
            return (T)pi.GetValue(obj, null);
        }
    }

public class CustomTableNameConvention : IClassConvention
{
    // Use this to set schema to specific value
    public void Apply(FluentNHibernate.Conventions.Instances.IClassInstance instance)
    {
        instance.Schema("My_NEw_Schema");
        instance.Table(instance.EntityType.Name.CamelToUnderscoreLower());
    }


    // Use this to alter the existing schema value.
    // note that Schema is a private property and you need reflection to get it
    public void Apply(FluentNHibernate.Conventions.Instances.IClassInstance instance)
    {          
        instance.Schema(instance.GetPrivatePropertyValue<string>("Schema").Replace(".", "_"));
        instance.Table(instance.EntityType.Name.CamelToUnderscoreLower());
    }
}

您只能使用其中一种 Apply 方法。

*更新2
我不知道我会推荐这个,但如果你想尝试一下,这似乎可行。更多反思:)

    public static void SetSchemaValue(this object obj, string schema)
    {

        var mapping_ref  = obj.GetType().GetField("mapping", BindingFlags.Instance | BindingFlags.IgnoreCase | BindingFlags.GetField | BindingFlags.NonPublic).GetValue(obj);

        var mapping = mapping_ref as ClassMapping;
        if (mapping != null)
        {
            mapping.Schema = schema;
        }
    }


    public void Apply(FluentNHibernate.Conventions.Instances.IClassInstance instance)
    {
        var schema = instance.GetPrivatePropertyValue<string>("Schema");
        if (schema == null)
        {
            instance.Schema("My_New_Schema");
        }
        else
        {
            instance.SetSchemaValue("My_New_Schema");
        }
    }

You can use a convention
http://wiki.fluentnhibernate.org/Conventions

*UPDATED

public static class PrivatePropertyHelper
    {
        // from http://stackoverflow.com/questions/1565734/is-it-possible-to-set-private-property-via-reflection
        public static T GetPrivatePropertyValue<T>(this object obj, string propName)
        {
            if (obj == null) throw new ArgumentNullException("obj");
            PropertyInfo pi = obj.GetType().GetProperty(propName, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
            if (pi == null) throw new ArgumentOutOfRangeException("propName", string.Format("Property {0} was not found in Type {1}", propName, obj.GetType().FullName));
            return (T)pi.GetValue(obj, null);
        }
    }

public class CustomTableNameConvention : IClassConvention
{
    // Use this to set schema to specific value
    public void Apply(FluentNHibernate.Conventions.Instances.IClassInstance instance)
    {
        instance.Schema("My_NEw_Schema");
        instance.Table(instance.EntityType.Name.CamelToUnderscoreLower());
    }


    // Use this to alter the existing schema value.
    // note that Schema is a private property and you need reflection to get it
    public void Apply(FluentNHibernate.Conventions.Instances.IClassInstance instance)
    {          
        instance.Schema(instance.GetPrivatePropertyValue<string>("Schema").Replace(".", "_"));
        instance.Table(instance.EntityType.Name.CamelToUnderscoreLower());
    }
}

You must use only one of he Apply methods.

*UPDATE 2
I don't know I would recommend this but if you like to experiment this seems to work. Even more reflection :)

    public static void SetSchemaValue(this object obj, string schema)
    {

        var mapping_ref  = obj.GetType().GetField("mapping", BindingFlags.Instance | BindingFlags.IgnoreCase | BindingFlags.GetField | BindingFlags.NonPublic).GetValue(obj);

        var mapping = mapping_ref as ClassMapping;
        if (mapping != null)
        {
            mapping.Schema = schema;
        }
    }


    public void Apply(FluentNHibernate.Conventions.Instances.IClassInstance instance)
    {
        var schema = instance.GetPrivatePropertyValue<string>("Schema");
        if (schema == null)
        {
            instance.Schema("My_New_Schema");
        }
        else
        {
            instance.SetSchemaValue("My_New_Schema");
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文