在我的域中,员工和部门具有一对多的双向关系; 为了让子Employee同步这个,我有一个'内部'访问字段,用于部门中的Employees的Set(Iesi for NHibernate),否则将只读公开.像这样:
系类:
protected internal ISet<Employee> _staff;
public virtual ReadOnlyCollection<Employee> Staff {
get { return new List<Employee>(_staff).AsReadOnly(); }
}
public virtual void AddStaff(Employee emp) {
emp.Department = this; }
}
Run Code Online (Sandbox Code Playgroud)
员工类:
private Department _department;
public virtual Department Department {
set {
// check valid value, etc.
value._staff.Add(this);
}
}
Run Code Online (Sandbox Code Playgroud)
我在我的(FNH)映射AsField(Prefix.Underscore)中进行访问,但由于我无法使Department._staff字段虚拟NH不满意.我想我可以使该字段成为虚拟属性并强制提供它,但这感觉就像我让域类过分意识到持久性.
我正在学习NH和FNH,我知道我需要一个很好的关系映射入门,但我对这篇文章的主要问题是我的域类中的逻辑:
1)这是一个很好的c#编程模式,用于一个只读集合双向关系?
2)使NHibernate更有用的最佳方法是什么?
感谢分享!
Berryl
我在Fluent NHibernate中遇到了映射错误.当我明确指定列时,为什么还在寻找_id?
Invalid column name 'Account_id'.
[GenericADOException: could not initialize a collection: [ProtoStack.Business.Entities.Account.LedgerEntries#1][SQL: SELECT ***ledgerentr0_.Account_id*** as Account5_1_, ledgerentr0_.Id as Id1_, ledgerentr0_.Id as Id43_0_, ledgerentr0_.LedgerEntryDate as LedgerEn2_43_0_, ledgerentr0_.Amount as Amount43_0_, ledgerentr0_.AccountId as AccountId43_0_ FROM dbo.LedgerEntry ledgerentr0_ WHERE ledgerentr0_.Account_id=?]]
Run Code Online (Sandbox Code Playgroud)
我已明确指定该列为"AccountId".
public class AccountMap : ClassMap<Account>
{
public AccountMap()
{
Table("dbo.Account");
Id(x => x.Id)
.Column("Id");
Map(x => x.Code);
Map(x => x.Name);
Map(x => x.Description);
Map(x => x.Category);
References(x => x.Group)
.Column("AccountGroupId");
HasMany(x => x.LedgerEntries)
.Inverse()
.Cascade.All();
}
}
public class LedgerEntryMap : ClassMap<LedgerEntry> …Run Code Online (Sandbox Code Playgroud) 我想用我的asp.net mvc web应用程序使用Fluent nhibernate进行数据访问......任何描述Fluent nHibernate与asp.net MVC web应用程序设置的好文章......
我正在使用Fluent(1.1.0)NHibernate(2.1.2),并且我有一个(子)子类,其中包含对另一个类的多对多引用:
(子)子类 - <交叉表> - 其他类
要么
FloorplanObject(基类)
几何(子类)
展位(SubSubclass) - <ExhibitorStand> - 参展商
基类:
public class FloorplanObject
{
public int Id { get; set; }
public string Name { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
基类映射:
class FloorplanObjectMap : ClassMap<FloorplanObject>
{
public FloorplanObjectMap()
{
Id(x => x.Id);
Map(x => x.Name);
}
}
Run Code Online (Sandbox Code Playgroud)
子类:
public class Geometry : FloorplanObject
{
public virtual List<float> Positions { get; set; }
public Geometry()
{
Positions = new List<float>();
}
}
Run Code Online (Sandbox Code Playgroud)
子类映射:
public class GeometryMap : …Run Code Online (Sandbox Code Playgroud) 这是我的第一个流畅的hibernate项目.我在hibernate和nhibernate方面经验很少.
这个上下文对我来说是全新的,因为这是一个Web应用程序项目.所以我有我的webapp项目,网上发现了大部分流利的nhibernate.所以我有这个实体:
namespace myproject.model
{
public class Request
{
public virtual string Id { get; private set; }
public virtual Route route { get; set; }
public virtual int code { get; set; }
}
}
namespace myproject.model
{
public class Route
{
public virtual string Id { get; private set; }
public virtual string client_id { get; set; }
public virtual IList<Request> requests { get; set; }
public Route()
{
requests = new List<Request>();
}
}
}
//Mapping are …Run Code Online (Sandbox Code Playgroud) 我已经分配了一个使用NHibernate的新项目.我可以在sql中轻松编写的查询让我完全难以理解如何在linq中执行此操作,这就是我被告知要执行此操作的方式.
所以,这是查询:
select ts.BatchID, COUNT(distinct ts.UniqID) SurveyCount
from TeleformStaging.TeleformStaging ts
where ts.IsRescan = 0
and not exists (select bfr.BatchID
from TeleformStaging.BatchesForRescan bfr
where bfr.BatchTrack = ts.BatchID)
group by ts.BatchID
order by ts.BatchID
Run Code Online (Sandbox Code Playgroud)
我相信我可以得到'分组'部分,但不知道子查询.
谢谢你的任何建议......
我正在尝试在ServiceStack中使用FluentNHibernate和Funq IoC容器,基于每个请求的会话,我遇到了一个问题,在第二次请求我的服务时,我得到一个ObjectDisposedException.Funq不应该为每个请求创建一个新的Session吗?
我的理解是,通过在Funq中使用ReusedWithin(ReuseScope.Request),每个请求都会得到一个新的ISession,但这只发生在第一个请求中.在我的AppHost中,我有以下内容:
public static NH.ISession CurrentSession
{
get
{
SessionFactory = GetFactory();
NH.ISession session = SessionFactory.OpenSession();
return session;
}
}
private static NH.ISessionFactory GetFactory()
{
return Fluently.Configure().Database(MsSqlConfiguration.MsSql2008
.ConnectionString(ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString)).Mappings(m =>
{ m.FluentMappings.AddFromAssemblyOf<Case>(); })
.BuildSessionFactory();
}
Run Code Online (Sandbox Code Playgroud)
并注册容器:
container.Register<NH.ISession>(c => CurrentSession).ReusedWithin(Funq.ReuseScope.Request);
container.Register<ILog>(c => LogManager.GetLogger(GetType()));
Run Code Online (Sandbox Code Playgroud) 我使用S#arpLite开发webapp来构建查询从许多表中获取列表.使用NHibernate版本3.3.1.4000
我在应用程序运行时遇到错误,例如
`NHibernate System.NotSupportedException Specified method is not supported. {Name = "PolymorphicQuerySourceDetector" FullName = "NHibernate.Hql.Ast.ANTLR.PolymorphicQuerySourceDetector"}
at NHibernate.Hql.Ast.ANTLR.PolymorphicQuerySourceDetector.GetClassName(IASTNode querySource)
at NHibernate.Hql.Ast.ANTLR.PolymorphicQuerySourceDetector.Process(IASTNode tree)
at NHibernate.Hql.Ast.ANTLR.AstPolymorphicProcessor.Process()
at NHibernate.Hql.Ast.ANTLR.AstPolymorphicProcessor.Process(IASTNode ast, ISessionFactoryImplementor factory)
at NHibernate.Hql.Ast.ANTLR.ASTQueryTranslatorFactory.CreateQueryTranslators(IASTNode ast, String queryIdentifier, String collectionRole, Boolean shallow, IDictionary`2 filters, ISessionFactoryImplementor factory)
at NHibernate.Hql.Ast.ANTLR.ASTQueryTranslatorFactory.CreateQueryTranslators(String queryIdentifier, IQueryExpression queryExpression, String collectionRole, Boolean shallow, IDictionary`2 filters, ISessionFactoryImplementor factory)
at NHibernate.Engine.Query.HQLExpressionQueryPlan.CreateTranslators(String expressionStr, IQueryExpression queryExpression, String collectionRole, Boolean shallow, IDictionary`2 enabledFilters, ISessionFactoryImplementor factory)
at NHibernate.Engine.Query.HQLExpressionQueryPlan..ctor(String expressionStr, IQueryExpression queryExpression, String collectionRole, Boolean shallow, IDictionary`2 enabledFilters, ISessionFactoryImplementor factory) … 我正在使用Fluent Nhibernate。映射类如下所示:
public class CategoryMap : ClassMap<Category>
{
public CategoryMap()
{
Id(x => x.CategoryId).UniqueKey("CategoryId").GeneratedBy.Increment();
Map(x => x.CategoryName).Not.Nullable();
References(x => x.ParentCategory).Column("ParentCategoryId").Nullable();
}
}
Run Code Online (Sandbox Code Playgroud)
它像我需要的那样创建表,但是在SSMS中,我看到的CategoryId不是身份。
为什么自动递增(身份)无效?
c# ×6
nhibernate ×4
linq ×2
.net ×1
asp.net ×1
asp.net-mvc ×1
funq ×1
mapping ×1
schemaexport ×1
servicestack ×1
sql-server ×1