似乎以下场景应该并不少见,但我无法弄清楚如何在 FluenNHibernate 中处理它:
public class Product: BaseEntity
{
public Product()
{
Categories = new List<Category>();
}
public virtual IList<Category> Categories { get; set; }
...
}
public enum Categories
{
Classic = 1,
Modern = 2,
Trendy = 3,
...
}
Run Code Online (Sandbox Code Playgroud)
所以,我需要一个 ProductCategories 表,它允许我将一个产品映射到多个类别,但我认为 NHibernate 不会处理这个问题,除非我有一个实际的 Categories 类和一个指定了多对多关系的 Categories 表。这有很多原因是不可取的,其中最重要的是它太过分了。
我正在使用 AutoMapper - 我可以通过什么方式覆盖以使其工作?
谢谢!
我尝试通过 FluentNHibernate 在 F# 项目中配置 NHibernate。
static member GetNHibernateConfig =
Fluently.Configure()
.Database(MsSqlConfiguration.MsSql2008
.ConnectionString(fun c -> c.FromConnectionStringWithKey("connectionString") |> ignore)
.ShowSql())
Run Code Online (Sandbox Code Playgroud)
Visual Studio 突出显示“c.FromConnectionStringWithKey”并显示错误:
根据该程序点之前的信息查找不确定类型的对象。在这个程序点之前可能需要一个类型注释来约束对象的类型。这可以允许解决查找。
我有一个SQL数据库和一个具有相同模式的Oracle数据库.
因此,我想将我的模型类用于两个数据库,我将要做的就是更改Fluent NHibernate配置中的数据库连接字符串.
我有一些列的数据库字符数据类型,但在我的模型类中,我将它们设置为字符串,但是当它们从查询返回时,它们填充了空格.
当我使用这些列查询数据库时,如何在不引起问题的情况下返回它们,因为它们需要匹配固定长度规范.
我有datetime列,如果没有提供,我想设置一个默认值.
所以在我的流畅映射中,我做到了
Map(x => x.EndOfTerm).Default("5/21/2011").Not.Nullable();
Run Code Online (Sandbox Code Playgroud)
然而,每当我尝试保存没有指定EndOfTerm的东西时,它就会崩溃
SqlDateTime overflow. Must be between 1/1/1753 12:00:00 AM and 12/31/9999 11:59:59 PM.
Run Code Online (Sandbox Code Playgroud)
当我给EndOfTerm一个值时,它不再崩溃.
我正在使用 Fluent NHibernate。我在运行时有一个对象,其中包含可能已填充或未填充的惰性集合/属性。我计划序列化该对象,并需要在我这样做之前填充所有集合/属性。如何在运行时“热切加载”我的对象?
我无法理解为什么这不起作用.我有两个实体,彼此之间存在多对多的关系.
public class User : BaseEntity
{
public User()
{
Roles = new List<Role>();
}
[DisplayName("UserName")]
[Required]
[StringLength(20, MinimumLength = 5)]
public virtual string UserName { get; set; }
[DisplayName("Password")]
[Required]
[DataType(DataType.Password)]
public virtual string Password { get; set; }
[DisplayName("Email Address")]
[Required]
[DataType(DataType.EmailAddress)]
public virtual string Email { get; set; }
public virtual IList<Role> Roles { get; set; }
}
public class Role : BaseEntity
{
public Role()
{
Users=new List<User>();
}
[Required]
[DisplayName("Role Name")]
public virtual string RoleName …Run Code Online (Sandbox Code Playgroud) 我们有一个Web应用程序,我们使用NHibernate作为ORM,现在我们需要在一些实体上添加审计来跟踪谁改变了什么.但是,我还没有设法找出处理它的最佳和最简单的方法.
如果可能的话,我希望审计信息进入每个实体的单独表,如下所示(伪sql):
create table MethodStatus (MethodId, Enabled)
create table MethodStatusAudit (MethodId, Enabled, AdminId, Date, ChangeType(U,D,I))
Run Code Online (Sandbox Code Playgroud)
问题是如何使用(Fluent)NHibernate设置它,以便它易于管理?
我最初的想法是使用IPreInsertEventListener/IPreUpdateEventListener/IPreDeleteEventListener并让我的可审计对象实现IAuditable接口,然后执行某些操作.但我无法弄清楚如何保存审计..
public interface IAuditable {}
public class MethodStatus : IAuditable
{
public virtual int MethodId { get; set; }
public virtual bool Enabled { get; set; }
}
public class MethodStatusMap : ClassMap<MethodStatus>
{
public MethodStatusMap()
{
Id(x => x.MethodId);
Map(x => x.Enabled);
}
}
public bool OnPreInsert(PreInsertEvent @event)
{
var e = @event.Entity as IAuditable;
if (e != null)
//save audit.. but how? …Run Code Online (Sandbox Code Playgroud) 我正在尝试在班级中映射以下受保护的集合:
public class AddressList
{
protected readonly IList<Address> addresses = new List<Address>();
}
Run Code Online (Sandbox Code Playgroud)
使用以下映射:
HasMany<Address>(list => Reveal.Member<AddressList>("addresses"));
Run Code Online (Sandbox Code Playgroud)
但是我一直在获取NHibernate.MappingException
NHibernate.MappingException : Could not compile the mapping document:
(XmlDocument) > NHibernate.PropertyNotFoundException
: Could not find a getter for property '**Member**' in class
Run Code Online (Sandbox Code Playgroud)
但是,正如您在Reveal.Member中看到的那样,我要指出它应查找的属性名称应该是“地址”
我发现了这个问题,用户似乎和我有同样的问题,只是一个老问题
我也尝试过这里给出的解决方案
该网页是一个简单的注册类型页面.每个用户必须填写大约200个字段.有1个父表和6个子表.每个表信息都显示在单独的AJAX选项卡面板中,详细信息将保存在每个选项卡导航中.我已经在Intranet中托管了该网站,并且同时由100个用户输入了详细信息.最初一切都进展顺利,条目被保存在数据库中,但有一段时间后,AJAX加载图像被显示,网站似乎非常慢.用户的详细信息未保存在数据库中.最后Firefox浏览器崩溃了一些人,有些应用程序甚至没有注销.该网站被绞死了!我也重置了IIS,一切都持续了几分钟,同样的问题又开始了!我使用NHibernate框架,AJAX,ASP.Net 4.0作为前端,SQL server 2008作为后端.
以下是日志文件中的错误详细信息:
2014-08-11 10:05:40,953 [22] INFO ASP.global_asax servername : -
************************* 11/08/2014 10:05 **************************** IP Address: xx.x.xxx.xx Exception Type: System.Web.HttpException Exception: Request timed out. Source:
**************************************************************************
2014-08-11 10:05:40,984 [39] INFO ASP.global_asax servername : -
************************* 11/08/2014 10:05 ****************************`
IP Address: xx.x.xxx.xx
Exception Type: System.Web.HttpException
Exception: The client disconnected.
Source: System.Web
Stack Trace:
at System.Web.UI.ViewStateException.ThrowError(Exception inner, String persistedState, String errorPageMessage, Boolean macValidationError)
at System.Web.UI.ViewStateException.ThrowViewStateError(Exception inner, String persistedState)
at System.Web.UI.ClientScriptManager.EnsureEventValidationFieldLoaded()
at System.Web.UI.ClientScriptManager.ValidateEvent(String uniqueId, String argument)
at System.Web.UI.Control.ValidateEvent(String uniqueID, String …Run Code Online (Sandbox Code Playgroud) 在DB2 LUW 9.7数据库上使用FluentNhibernate 1.3.0.0,NHibernate 3.3.1.4000.
我想从一个表/实体中获取一些不同的数据.在SQL中,很简单:
select distinct Corporation, CalculationDate, ValuationRule
from MySchema.MyTable
where State == 0
Run Code Online (Sandbox Code Playgroud)
现在,我正在尝试使用Linq获取这些数据,但它无法正常工作......
首先尝试使用select:
var result = Session.Query<MyEntity>()
.Where( x => x.State == State.Pending)
.Select(
x =>
new
{
Corporation = x.Corporation,
CalculationDate = x.CalculationDate,
ValuationRule = x.ValuationRule,
}).Distinct().ToList();
Run Code Online (Sandbox Code Playgroud)
产生的异常:此SelectClauseVisitor不支持表达式类型'NhDistinctExpression'.
第二次尝试,使用Groupby并只获取密钥:
var result = Session.Query<MyEntity>()
.Where( x => x.State == State.Pending)
.GroupBy(
x =>
new
{
Corporation = x.Corporation,
CalculationDate = x.CalculationDate,
ValuationRule = x.ValuationRule,
}).Select( x => x.Key).ToList();
Run Code Online (Sandbox Code Playgroud)
结果异常:"无法执行查询".抱怨选择术语中所述的group by子句中缺少的另一个字段"Model".这让我很困惑,因为表中存在指定的字段,但我不想在该用例中使用该字段...
我错过了什么?
nhibernate ×7
c# ×6
.net ×1
ajax ×1
asp.net ×1
asp.net-4.0 ×1
audit ×1
f# ×1
function ×1
lazy-loading ×1
linq ×1
orm ×1
sqlite ×1