Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5937065
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T15:29:13+00:00 2026-05-22T15:29:13+00:00

How do I make a one to one mapping. public class Setting { public

  • 0

How do I make a one to one mapping.

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; }
}

Setting Class map:

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);
}

Student Class map

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; }
}

Now every time I try to create a settings object and save it to the database it crashes.

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.

What am I missing?

Edit

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();

I get these error for both ways

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: }

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T15:29:14+00:00Added an answer on May 22, 2026 at 3:29 pm

    There are two basic ways how to map bidirectional one-to-one association in NH. Let’s say the classes look like this:

    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; }
    }
    

    Setting class is a master in the association (“aggregate root”). It is quite unusual but it depends on problem domain…

    Primary key association

    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();
    }
    

    and a new setting instance should be stored:

            var setting = new Setting();
    
            setting.Student = new Student();
            setting.Student.Name = "student1";
            setting.Student.Setting = setting;
            setting.Name = "setting1";
    
            session.Save(setting);
    

    Foreign key association

    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");
    }
    

    Primary key association is close to your solution. Primary key association should be used only when you are absolutely sure that the association will be always one-to-one. Note that AllDeleteOrphan cascade is not supported for one-to-one in NH.

    EDIT: For more details see:

    http://fabiomaulo.blogspot.com/2010/03/conform-mapping-one-to-one.html

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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.