- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
如何进行一对一映射。
public class Setting
{
public virtual Guid StudentId { get; set; }
public virtual DateFilters TaskFilterOption { get; set; }
public virtual string TimeZoneId { get; set; }
public virtual string TimeZoneName { get; set; }
public virtual DateTime EndOfTerm { get; set; }
public virtual Student Student { get; set; }
}
设置类图:
public SettingMap()
{
// Id(Reveal.Member<Setting>("StudentId")).GeneratedBy.Foreign("StudentId");
//Id(x => x.StudentId);
Map(x => x.TaskFilterOption)
.Default(DateFilters.All.ToString())
.NvarcharWithMaxSize()
.Not.Nullable();
Map(x => x.TimeZoneId)
.NvarcharWithMaxSize()
.Not.Nullable();
Map(x => x.TimeZoneName)
.NvarcharWithMaxSize()
.Not.Nullable();
Map(x => x.EndOfTerm)
.Default("5/21/2011")
.Not.Nullable();
HasOne(x => x.Student);
}
学生类(class)图
public class StudentMap: ClassMap<Student>
{
public StudentMap()
{
Id(x => x.StudentId);
HasOne(x => x.Setting)
.Cascade.All();
}
}
public class Student
{
public virtual Guid StudentId { get; private set; }
public virtual Setting Setting { get; set; }
}
现在每次我尝试创建一个设置对象并将其保存到数据库时它都会崩溃。
Setting setting = new Setting
{
TimeZoneId = viewModel.SelectedTimeZone,
TimeZoneName = info.DisplayName,
EndOfTerm = DateTime.UtcNow.AddDays(-1),
Student = student
};
The INSERT statement conflicted with the FOREIGN KEY constraint "FK_Settings_Students". The conflict occurred in database "Database", table "dbo.Students", column 'StudentId'.The statement has been terminated.Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.Data.SqlClient.SqlException: The INSERT statement conflicted with the FOREIGN KEY constraint "FK_Settings_Students". The conflict occurred in database "Database", table "dbo.Students", column 'StudentId'.The statement has been terminated.
我错过了什么?
编辑
public class StudentMap: ClassMap<Student>
{
public StudentMap()
{
Id(x => x.StudentId)
.GeneratedBy.Guid();
HasOne(x => x.Setting)
.PropertyRef("Student")
.Cascade.All();
}
}
public class SettingMap: ClassMap<Setting>
{
public SettingMap()
{
Id(x => x.StudentId)
.GeneratedBy.Guid();
Map(x => x.TaskFilterOption)
.Default(DateFilters.All.ToString())
.NvarcharWithMaxSize().Not.Nullable();
Map(x => x.TimeZoneId)
.NvarcharWithMaxSize().Not.Nullable();
Map(x => x.TimeZoneName)
.NvarcharWithMaxSize().Not.Nullable();
Map(x => x.EndOfTerm)
.Default("5/21/2011").Not.Nullable();
References(x => x.Student).Unique();
}
}
Setting setting = new Setting
{
TimeZoneId = viewModel.SelectedTimeZone,
TimeZoneName = info.DisplayName,
EndOfTerm = DateTime.UtcNow.AddDays(-1),
Student = student
};
studentRepo.SaveSettings(setting);
studentRepo.Commit();
两种方式我都得到这些错误
Invalid index 5 for this SqlParameterCollection with Count=5. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.IndexOutOfRangeException: Invalid index 5 for this SqlParameterCollection with Count=5. Source Error: Line 76: using (ITransaction transaction = session.BeginTransaction()) Line 77: { Line 78: transaction.Commit(); Line 79: } Line 80: }
最佳答案
在 NH 中映射双向一对一关联有两种基本方法。假设类如下所示:
public class Setting
{
public virtual Guid Id { get; set; }
public virtual Student Student { get; set; }
}
public class Student
{
public virtual Guid Id { get; set; }
public virtual Setting Setting { get; set; }
}
设置类是关联中的主控(“聚合根”)。这很不寻常,但它取决于问题域......
主键关联
public SettingMap()
{
Id(x => x.Id).GeneratedBy.Guid();
HasOne(x => x.Student).Cascade.All();
}
public StudentMap()
{
Id(x => x.Id).GeneratedBy.Foreign("Setting");
HasOne(x => x.Setting).Constrained();
}
并且应该存储一个新的设置实例:
var setting = new Setting();
setting.Student = new Student();
setting.Student.Name = "student1";
setting.Student.Setting = setting;
setting.Name = "setting1";
session.Save(setting);
外键关联
public SettingMap()
{
Id(x => x.Id).GeneratedBy.Guid();
References(x => x.Student).Unique().Cascade.All();
}
public StudentMap()
{
Id(x => x.Id).GeneratedBy.Guid();
HasOne(x => x.Setting).Cascade.All().PropertyRef("Student");
}
主键关联接近您的解决方案。仅当您绝对确定关联始终是一对一时,才应使用主键关联。请注意,NH 中的一对一不支持 AllDeleteOrphan 级联。
编辑:有关更多详细信息,请参阅:
http://fabiomaulo.blogspot.com/2010/03/conform-mapping-one-to-one.html
关于c# - Fluent nhibernate 一对一映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6085568/
我在使用一对一映射时遇到问题。我搜索了互联网并找到了许多解决方案,但没有一个令人满意。大多数示例都带有将父实例存储在子类中的开销。 我只想在具有外键约束关系的子类中使用父 ID,但不想在子类中保留任何
我有以下设置: Micronaut 3.x Java 15 我要更新的实体: @Setter @Getter @Entity @ToString @Table(name = "single_choic
我正在使用AVAudioPlayer制作MP3播放器。我有多种MP3声音,想一一播放。以下是我的应用程序的逻辑: ///// For playing 1st sound mp3Player = [[A
所以这就是问题所在。我有 2 个模型: 裁判级别和裁判 两个都有: class RefereeLevel(models.Model): level = models.PositiveSmall
我想将数组添加到列表或多维数组(不是一次全部...)。但是我真的不明白为什么这应该那么难。 可以说我有这个: string[] a = { "h", "b"}; string[] b
我有一个长度为 1000 的数字序列,我将其分成 100 个长度的序列。所以我最终得到了 901 个长度为 100 的序列。让前 900 个序列为 trainX。 trainY 是这些序列中的第 2
关键字:association 一对一映射(一个班级只有一个班主任) ?
所以,这是我第一次学习计算机语言。我选择了python和django。现在,我了解了 python 和 django 的许多基本概念。我可以使用 View 和所有其他内容创建新页面。但我仍然对这些关系
在一对一映射中,我编写了以下代码行。 @GenericGenerator(name = "generator", strategy = "foreign", parameters = @Paramet
我有两个如下所示的实体 @Data @EqualsAndHashCode(callSuper = true) @Entity @Table(name = "foo") @Audited @AuditO
我的问题很简单.. 假设有 2 个类..书籍和作者 假设一本书只能由一位作者撰写。 一个作家可以写很多本书。 假设作者有唯一的名字。 [两个作者不能同名] 现在..假设所有 hibernate\JPA
我正在尝试创建一个实体,如下所示 @Data public class Person { @Id private String id; @OneToMany(mapp
我有一个包含字段的Project表 ID PROJECT_BASELINE_ATTRIBUTES_ID (FK for table PROJECT_BASELINE_ATTR) 这个表有如下映射
我正在学习基本的 hibernate 教程。我正在尝试在 2 个表之间建立一对一的关系。当我尝试插入 Client 表时它起作用了,但是当我尝试做相反的事情时(插入 Facture 表)我得到了这个异
我已经在 hibernate 3 中使用注释完成了一对一映射,我有两张表“组”和“类别”。类别是预定义的。当用户选择类别和组时,CategoryId和goupid应该只插入组表中。 那么应该如何映射。
我使用Linux服务Fedora 4.14.33-51.37.amzn1.x86_64。我想使用 NAT 1 对 1。例如是否相同problem我的方案是:我的服务器有两个网络接口(interface
我正在尝试与实体 Revision 创建一对一、自引用、双向关系(哇),看起来像这个: /** * @Entity() * @Table(name="rev") */ class Revisio
我需要两个实体之间的链接,所以我使用一对一 @Entity @Table(name = "T_USER") public class User implements Serializable {
一对一: 一对一的关系极为一个数据仅对应一个数据,用下图的结构图可以帮助理解: 下面用代码实现一下,首先要创建工程项目如下: 接着,我们定义模型: 来到models.py文件,创建两
假设我有5列。 pd.DataFrame({ 'Column1': [1, 2, 3, 4, 5, 6, 7, 8, 9], 'Column2': [4, 3, 6, 8, 3, 4, 1, 4, 3
我是一名优秀的程序员,十分优秀!