- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有两个表在数据库级别定义父子关系。父表有单列主键,子表有复合主键,其中一列引用父表:
application table tasks table
= task_id (CK) -------> = taskid (PK)
= user_id (CK)
= transaction_id (CK)
仅供引用,user_id
和transaction_id
列不引用其他表。
我正在尝试为 C# 中的两个实体设置 Fluent NHibernate 映射:
public class Application
{
public virtual string UserId { get; set; }
public virtual string TransactionId { get; set; }
public virtual string TaskId { get; set; }
public virtual Task Task { get; set; }
}
public class Task
{
public string Id { get; set; }
}
一个Application
有一个Task
,但是一个Task
有很多Application
。我正适应这种关系。
internal class ApplicationMap : ClassMap<Application>
{
public ApplicationMap() : base()
{
Schema(...);
Table(...);
CompositeId()
.KeyProperty(app => app.UserId, "user_id")
.KeyReference(app => app.Task, "task_id")
.KeyProperty(app => app.TransactionId, "transaction_id");
// No explicit mapping defined for "task_id"
// Other columns mapped, but omitted for brevity
}
}
internal class TaskMap : ClassMap<Task>
{
public TaskMap()
{
Schema(DbSchema.SchemaName);
Table(DbSchema.TableName);
Id(task => task.Id, "taskid");
// Other columns mapped, but omitted for brevity
// Relations
HasMany(task => task.Applications);
}
}
将新的 Application
插入数据库后,出现以下异常:
NHibernate.QueryException: could not resolve property: TaskId of: Application.
我尝试为 TaskId
属性添加到 ApplicationMap
的显式映射,但我得到了 super 有用的信息“Index was out of range. Must be non-negative and less比集合的大小。”来自 NHibernate 的异常:
internal class ApplicationMap : ClassMap<Application>
{
public ApplicationMap() : base()
{
Schema(...);
Table(...);
CompositeId()
.KeyProperty(app => app.UserId, "user_id")
.KeyReference(app => app.Task, "task_id")
.KeyProperty(app => app.TransactionId, "transaction_id");
Map(app => app.TaskId, "task_id");
// Other columns mapped, but omitted for brevity
}
}
看完Fluent NHibernate compositeid to mapped class ,我不确定还能尝试什么。这个问题和这个问题的区别在于,子表上的外键列确实需要映射到实体 (Application.TaskId
) 中。
我一直在搜索 Fluent NHibernate 文档,但很难找到任何涉及复合主键的内容,尤其是涉及到与其他表的关系时。
TaskId
和 Task
我确实偶尔需要 Application.Task
,但不是很频繁。但是,应用程序表上的组合键用作与应用程序表相关的所有其他表的组合外键引用。 TaskId
属性将被访问很多,我想避免对应用程序和任务表进行 JOIN 查询只是为了获取应用程序中已有的值表。
我在 NHibernate 中为这个映射和存储库编写了一个单元测试,但它失败了:
var app = new Application(user)
{
TaskId = "...",
// More properties being set...
};
db.Web.Applications.Create(app);
db.SaveChanges();
var actual = db.Web.Applications.Find(app.UserId, app.TaskId, app.TransactionId);
// Test was failing here
Assert.IsNotNull(actual.Task, "No task found");
真正的问题似乎是新插入记录的 Task
属性为空,并且在从同一个 NHibernate session (这经过一些研究是预期的行为)。
我经历了多次映射迭代,实际上最初的映射确实存在问题。我只是“一直有问题”,因为我不明白 NHibernate 在插入新记录时的行为方式。
最佳答案
我认为您对 TaskClassMap
的映射需要如下所示:
public class TaskClassMap : ClassMap<Task>
{
public TaskClassMap()
{
Table("Task");
Id(task => task.Id, "taskid");
HasMany(c => c.Applications)
.KeyColumn("task_id");
}
}
如果您没有指定特定的列名称 (.KeyColumn
),nhibernate 会尝试使用在这种情况下为 TaskId
的约定。
此外,您收到下面臭名昭著的错误的原因还在于您试图在同一映射 (ApplicationMap
) 中两次映射同一列 (task_id
):
Index was out of range. Must be non-negative and less than the size of the collection.
CompositeId()
.KeyProperty(app => app.UserId, "user_id")
.KeyReference(app => app.Task, "task_id")
.KeyProperty(app => app.TransactionId, "transaction_id");
Map(app => app.TaskId, "task_id");
The TaskId property is going to be access a lot, and I'd like to avoid a JOIN query on the application and tasks table just to get a value already in the application table.
还要评论上面的声明,如果您只访问 Application.Task.Id
,nhibernate 将不会查询数据库。在执行延迟加载时,nhibernate 会为这种类型的关系创建一个代理对象,其中存储在内存中的唯一字段是主键(Task.Id
)。因此,如果您要访问该字段,它实际上不会访问数据库。如果您访问 id 之外的任何其他字段,它将触发对数据库的查询以获取剩余的值。就像你在评论中说的那样,这个值已经存储在 Application
表中,因此 nhibernate 不会查询 Task
表,直到你尝试访问一个值仅在该表中。
关于c# - 如何将一对一关系映射为 Fluent NHibernate 中复合键的一部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29307899/
这是我的 Fluent 模型 struct Ailment: PostgreSQLModel { enum Frequency: String , Content { case regular
我正在尝试使用 Fluent Nhibernate 自动映射一个简单的继承层次结构,并且我需要为每个表使用与其类稍有不同的名称(下划线而不是 Pascal 大小写)。这似乎是一个使用约定的明显地方。我
如何为没有标识列的表指定流畅的 NHibernate 映射? 我想要这样的东西: public sealed class CustomerNewMap : ClassMap, IMap { p
使用 FluentMigrator,有没有办法找出 MigrateUp() 函数是否确实会迁移某些东西,或者它是否已经是最新的? 最佳答案 没有简单的方法可以使用公共(public) api 判断 M
我正在使用 Fluent NHibernate,我喜欢它! 我有一个小问题:启动时间大约是 10 秒,我不知道如何优化 Fluent nHibernate 为了减少这个启动时间的问题,我把它放在一个线
我在 Fluent NHIbernate 中使用 AutoPersistenceModel 来映射我的所有实体,并且一切正常:D 但是,我的几个对象有 public virtual IList Com
我有一个数据库,我正在运行多个应用程序。我喜欢通过为每个应用程序创建模式来分隔表。对于我最新的应用程序,我使用的是 FluentNHibernate。似乎我的大部分管道都是正确的,但是当我尝试查询其中
应用程序有许多扩展程序集,它们包含其类的映射。我需要为这些映射中的所有(基本、连接、多对多等)表名添加前缀。 例如 Assembly: ~/bin/Extensions/Foo.dll
您好,我很好奇 DDD 是如何使用 Fluent Nhibernate 真正实现的。例如,我有一个名为 User 的实体类和另一个名为 UserProfile 的类,就我而言,UserProfile
是否可以在 Fluent NHibernate 中映射来自多个程序集的实体? 我试过了 AutoPersistenceModel .MapEntitiesFromAssemblyOf() .AddEn
我有一个看起来像这样的基类: public abstract class MyBaseClass { public virtual DateTime UpdatedOn { get; set;
我有 Post 和 Comment 类,它们有一对多的关系,其中 Post 有一个评论列表。我如何将其映射为与 Fluent NHibernate 的单向关系,因为评论不需要知道其父 Post?目前,
我对如何查询模型对象的子对象并立即使用它感到困惑。我的Client包含数量Station child final class Client: PostgreSQLModel { var sta
目前我有一个表“ComponentAnalysis”和一个表“HistoryOfUse”,我正试图在 Fluent NHibernate 中进行映射。 一个成分分析应该只有1个使用历史,一个使用历史应
正如标题所说,我想知道我是否应该避免将 fluent nhibernate 用于生产代码,或者它是否足够成熟,可以“深入研究”? :) 最佳答案 FluentNHibernate API 尚未稳定下来
我正在尝试使用 Fluent NHibernate,我有几个问题。我发现缺少文档。 我知道 Fluent NHibernate/NHibernate 允许您自动生成数据库模式。人们通常只对测试/开发数
我正在使用 fluent-nhibernate 约定来映射我的实体: public class HasManyConvention : IHasManyConvention {
如何更改多列索引中的列顺序? IE: mapping.References(x => x.SomeReference).SetAttribute("index", "IX_index"); mappi
我需要像下面的代码一样创建一个外键: Create.ForeignKey().FromTable("TCGDocFiscalOpMedItem").ForeignColumn("IDCabecalho
我正在使用 Sharp 架构,并且在许多情况下都在实体中使用了值对象。这是一个明显的简单示例: public class Person : Entity { protected Person(
我是一名优秀的程序员,十分优秀!