我无法想出这一个...... 我曾尝试将FluentNhibernate与SQLCE4一起使用..我的配置在会话中看起来像这样:
public class FluentNHibernateFactory
{
private static ISessionFactory CreateSessionFactory()
{
return Fluently.Configure()
.Database(MsSqlCeConfiguration.Standard
.ConnectionString(ConfigurationManager.ConnectionStrings["SqlCeDatabase"].ConnectionString))
.Mappings(m => m.FluentMappings.AddFromAssemblyOf<ApplicationEntity>())
.BuildSessionFactory();
}
public static ISession GetOpenSession()
{
return CreateSessionFactory().OpenSession();
}
}
我的映射看起来像这样:
public class ApplicationMap : ClassMap<ApplicationEntity>
{
public ApplicationMap()
{
Table("Applications");
Id(x => x.Id).GeneratedBy.Assigned().Column("id");
Id(x => x.Alias).Column("alias");
Id(x => x.Name).Column("name");
Map(x => x.Created).Column("created");
Map(x => x.CreatedByUser).Column("createdBy");
Map(x => x.Updated).Column("updated");
Map(x => x.UpdatedByUser).Column("updatedBy");
}
}
最后..我的存储库看起来像这样......:
public class ApplicationRepository : IRepository<ApplicationEntity>
{
public void Add(ApplicationEntity entity)
{
using (var session = FluentNHibernateFactory.GetOpenSession())
{
using (ITransaction transaction = session.BeginTransaction())
{
session.Save(entity);
//TODO: Fix the add functionality
transaction.Commit();
}
}
}
public void Remove(ApplicationEntity entity)
{
throw new NotImplementedException();
}
public void Update(ApplicationEntity entity)
{
throw new NotImplementedException();
}
public IEnumerable<ApplicationEntity> GetAll()
{
using (var session = FluentNHibernateFactory.GetOpenSession())
{
using (ITransaction transaction = session.BeginTransaction())
{
return session.CreateCriteria<ApplicationEntity>().List<ApplicationEntity>().AsEnumerable<ApplicationEntity>();
}
}
}
public ApplicationEntity GetById(long id)
{
throw new NotImplementedException();
}
}
但是我只是无法将任何对象添加到我的数据库中..传递给Add方法的实体对象具有所有属性的值,它们似乎也是有效的属性..但是我确实有三个主键在表中,那可能是问题吗?
顺便说一句,表格如下:
我得到的错误是: {“无法插入:[LBi.CATT.Core.Domain.Entities.ApplicationEntity #Test] [SQL:INSERT INTO应用程序(创建,创建,更新,更新,名称)VALUES(?,?,?,?,?) ]“}
以及内部异常: {“数据转换失败。[OLE DB状态值(如果已知)= 2]”}
抱歉桌子格式不好..
提前致谢!
答案 0 :(得分:0)
以下是不合法的
Id(x => x.Id).GeneratedBy.Assigned().Column("id");
Id(x => x.Alias).Column("alias");
Id(x => x.Name).Column("name");
要么使用它(如果Id是主键)
Id(x => x.Id)
.GeneratedBy.Assigned()
.UnsavedValue(0)
.Column("id");
Map(x => x.Alias).Column("alias");
Map(x => x.Name).Column("name");
或者这个(如果Id,Alias,Name together是主键)
CompositeId()
.KeyProperty(x => x.Id, "id")
.KeyProperty(x => x.Alias, "alias")
.KeyProperty(x => x.Name, "name");