Fluent NHibernate、SQL Server 和字符串长度规范未按预期工作

发布于 2024-12-13 02:38:41 字数 1706 浏览 0 评论 0原文

我正在关注 Summer of NHibernate 教程,但我没有使用 xml 映射,而是使用 Fluent NHibernate 来进行映射。

我的Customer实体类是:

public class Customer
{
    public virtual int CustomerId { get; set; }

    public virtual string Firstname { get; set; }

    public virtual string Lastname { get; set; }   
}

对应的映射类是:

public class CustomerMap: ClassMap<Customer>
{
    public CustomerMap()
    {
        Id(x =>x.CustomerId);
        Map(x => x.Firstname).Length(50).Nullable();
        Map(x => x.Lastname).Length(50).Nullable();
        ImportType<CustomerFirstnameCounter>();
    }

}

我的DAO类是:

 public int AddCustomer( Customer customer )
    {

        using( ISession session = GetSession() )
        {
            using( ITransaction tx = session.BeginTransaction() )
            {
                try
                {
                    int newId = ( int ) session.Save( customer );
                    session.Flush();
                    tx.Commit();
                    return newId;
                }
                catch( GenericADOException )
                {
                    tx.Rollback();
                    throw;
                }
            }
        }

    }

最后我的测试是:

 [Test]
 public void AddCustomerThrowsExceptionOnFail()
 {
       // Arrange
       Customer customer = BuildInvalidCustomer();

       // Act
       _provider.AddCustomer( customer );

       // Assert

 }

测试运行时,没有抛出异常!所以我的第一个问题是是否有人能看出我的映射出了什么问题。

现在,在 dB 中,Firstname 字段设置为 varchar(50)。当我调试测试时,我看到数据已插入但被截断(我确实收到警告消息)。所以这可能表明
我没有正确设置 dB。谁能告诉我如何防止 SQL Server 中的数据截断?

I am following the Summer of NHibernate tutorials but I am not using the xml mappings but instead, I am making use of Fluent NHibernate to do the mappings.

My Customer entity class is:

public class Customer
{
    public virtual int CustomerId { get; set; }

    public virtual string Firstname { get; set; }

    public virtual string Lastname { get; set; }   
}

The corresponding mapping class is:

public class CustomerMap: ClassMap<Customer>
{
    public CustomerMap()
    {
        Id(x =>x.CustomerId);
        Map(x => x.Firstname).Length(50).Nullable();
        Map(x => x.Lastname).Length(50).Nullable();
        ImportType<CustomerFirstnameCounter>();
    }

}

My DAO class is:

 public int AddCustomer( Customer customer )
    {

        using( ISession session = GetSession() )
        {
            using( ITransaction tx = session.BeginTransaction() )
            {
                try
                {
                    int newId = ( int ) session.Save( customer );
                    session.Flush();
                    tx.Commit();
                    return newId;
                }
                catch( GenericADOException )
                {
                    tx.Rollback();
                    throw;
                }
            }
        }

    }

And finally my test is:

 [Test]
 public void AddCustomerThrowsExceptionOnFail()
 {
       // Arrange
       Customer customer = BuildInvalidCustomer();

       // Act
       _provider.AddCustomer( customer );

       // Assert

 }

When the test runs, no exception is thrown! So my first question is whether anyone can see what is wrong with my mapping.

Now, in the dB, the Firstname field is set as a varchar(50). When I debug the test, I see that the data is inserted but truncated (I do get warning messages). So this might indicate
that I haven't set the dB up properly. Can anyone point me in the direction of where to prevent this truncation of data in SQL Server?

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

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

发布评论

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

评论(2

恋竹姑娘 2024-12-20 02:38:41

这个答案应该对您有帮助。
我还将使用数据注释 StringLengthAttribute 来确保您的属性的验证

This answer should help you.
I will also use Data Annotation StringLengthAttribute to ensure validation of your properties

又爬满兰若 2024-12-20 02:38:41

.Length(50) 不会在运行时检查长度。仅当您从映射生成数据库模式时才使用此选项。

如果您希望验证值的长度,则必须手动执行此操作或使用一些验证框架,例如 NHibernate Validator

.Length(50) does not check lengths at run-time. This is only used if you are generating the database schema from the mappings.

If you wish to validate the length of values you will have to either do this manually or use some validation framework like NHibernate Validator

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文