c# - Fluent nhibernate 一对一映射

标签 c# nhibernate fluent-nhibernate

如何进行一对一映射。

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

http://ayende.com/blog/3960/nhibernate-mapping-one-to-one

关于c# - Fluent nhibernate 一对一映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6085568/

相关文章:

c# - NHibernate 级联 ="all-delete-orphan"正在删除非孤立行

C# Windows 服务 SqlCommand 挂起

c# - 使用作为字符串接收的 JSON 数组

c# - ComboBox 选定的文本或项目

c# - 处理遗留数据库时在 NHibernate 中建模多对一关系的最佳方法?

.net - 为 .NET 项目选择数据库和 ORM

nhibernate - 多对多集合中的 Nhibernate 和 Fluent Nhibernate 的内连接或右外连接

c# - 如何将 "var"用法(而不是显式类型)视为警告?

nhibernate - 带有 TransactionScope 和增量身份生成器的 SQLite

c# - 使用 Fluent NHibernate 将新项目添加到集合中不会在数据库中插入新行