创建生成数据库的测试时遇到问题 我阅读了几篇关于使用实例管道名称的文章。但我不知道怎么用!
它遵循我的应用程序的结构
用于NHibernate和LocalDB的测试
文件 App.config
<configSections>
<section name="hibernate-configuration" type="NHibernate.Cfg.ConfigurationSectionHandler, NHibernate" />
</configSections>
<appSettings>
<add key="FluentAssertions.TestFramework" value="mstest"/>
</appSettings>
<connectionStrings>
<clear/>
<add
name="DataNP"
connectionString="np:\\.\pipe\LOCALDB#53EDC7C5\tsql\query"
providerName="System.Data.SqlClient"/>
<add
name="Data"
connectionString="Data Source=(LocalDb)\Projects;Initial Catalog=sistema.creditodobrasil.test;Integrated Security=True;Connect Timeout=15;Encrypt=False;TrustServerCertificate=False"
providerName="System.Data.SqlClient"/>
</connectionStrings>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
<session-factory>
<property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
<property name="dialect">NHibernate.Dialect.MsSql2008Dialect</property>
<property name="connection.connection_string_name">DataNP</property>
<property name="show_sql"></property>
</session-factory>
</hibernate-configuration>
中的评论
文件 DatabaseRepositoryTest
[TestClass]
public class DatabaseRepositoryTest
{
protected ISession Session { get; private set; }
private static ISessionFactory _sessionFactory;
private static Configuration _configuration;
[TestInitialize]
public void SetUp()
{
_configuration = new NHibernate.Cfg.Configuration();
_sessionFactory = Fluently.Configure(_configuration)
.Mappings(m => m.FluentMappings.AddFromAssemblyOf<Usuario>())
.BuildSessionFactory();
Session = _sessionFactory.OpenSession();
var export = new SchemaExport(_configuration);
export.Execute(true, true, true, Session.Connection, null);
}
[TestMethod]
public void CreateAtPreenchidaPeloDatabase()
{
// Arrange
var usuario = new Usuario();
// Action
using (var session = _sessionFactory.OpenSession())
{
session.SaveOrUpdate(usuario);
session.Flush();
}
// Assert
usuario.CreateAt.Should().BeCloseTo(DateTime.Now, 200);
}
}
评论:http://chopapp.com/#i6bosycr
档案 UsuarioMap
public class UsuarioMap : ClassMap<Usuario>
{
public UsuarioMap()
{
Id( p => p.Id);
Map(p => p.Name).Not.Nullable().AsName();
Map(p => p.Email).Not.Nullable().AsEmail();
Map(p => p.EmailPessoal).Not.Nullable().AsEmail();
References(p => p.Status).Not.Nullable();
Map(p => p.BloquearAte).Nullable();
References(p => p.Photo).Nullable();
Map(p => p.DataNascimento).Nullable();
Map(p => p.CreateAt).AsCreateAt();
Map(p => p.UpdateAt).AsUpdateAt();
Table("Usuarios");
}
}
中的评论
使用Connectionstirng“数据”错误: No persister for: CreditoImobiliarioBB.Model.Usuario
在Fluently.Configure上使用Connectionstirng“DataNP”错误: Format of the initialization string does not conform to specification starting at index 0.
如何使用NHibernate和LocalDB MSTeste创建和配置生成数据库的测试停止?