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 7721793
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T04:00:56+00:00 2026-06-01T04:00:56+00:00

I have a one-to-many relationship between Foo and Bar and I cannot perform an

  • 0

I have a one-to-many relationship between Foo and Bar and I cannot perform an insert (I’ve only tried select and insert so far…select seems to be working just fine, insert fails every time).

Table Definitions:

CREATE TABLE [Bar](
[Id] [int] IDENTITY(1,1) NOT NULL,
[DateField] [datetime] NULL,
[StringField] [varchar](8000) NULL
CONSTRAINT [PK_Bar] PRIMARY KEY CLUSTERED([Id] ASC)) ON PRIMARY

CREATE TABLE [Foo](
[Id] [int] IDENTITY(1,1) NOT NULL,
[BarId] [int] NOT NULL,
[DateField] [date] NOT NULL,
[StringField] [varchar](100) NOT NULL,
[GorpId] [int] NOT NULL,
[BoolField] [bit] NOT NULL
CONSTRAINT [PK_Foo] PRIMARY KEY CLUSTERED([Id] ASC))

ALTER TABLE [dbo].[Foo]  WITH CHECK ADD  CONSTRAINT [FK_Foo_Bar] FOREIGN KEY([BarId])
REFERENCES [dbo].[Bar] ([Id])
GO

Class Definitions:

public class Foo
{
    public virtual int Id { get; set; }
    public virtual Bar Bar{ get; set; }
    public virtual DateTime DateField{ get; set; }
    public virtual string StringField{ get; set; }
    public virtual Gorp Gorp{ get; set; }
    public virtual bool BoolField{ get; set; }
}

public class Bar
{
    public virtual int Id { get; set; }
    public virtual DateTime DateField{ get; set; }
    public virtual string StringField{ get; set; }

    public virtual ICollection<Foo> Foos{ get; set; }

    public Bar()
    {
        Foos= new List<Foo>();
    }

    public virtual void AddFoo(Foo foo)
    {
        foo.Bar = this;
        Foos.Add(foo);
    }
}

Mappings:

public class FooMap : ClassMap<Foo>
{
    public FooMap()
    {
        Id(x => x.Id).UnsavedValue(0).GeneratedBy.Identity();
        Map(x => x.DateField);
        Map(x => x.BoolField);
        Map(x => x.StringField);
        References(x => x.Gorp)
          .Column("GorpId")
          .Class<Gorp>();
        References(x => x.Bar)
          .Column("BarId")
          .Not.Nullable();
    }
}

public class BarMap : ClassMap<Bar>
{
    Id(x => x.Id).UnsavedValue(0).GeneratedBy.Identity();
    Map(x => x.DateField);
    Map(x => x.StringField);

    HasMany<Foo>(x => x.Foos)
      .AsBag()
      .Cascade.SaveUpdate()
      .ForeignKeyConstraintName("FK_Foo_Bar")
      .Inverse()
      .KeyColumn("BarId")
      .Not.KeyNullable();
}

Generated XML:

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" default-access="property" auto-import="true" default-cascade="none" default-lazy="true">
    <class xmlns="urn:nhibernate-mapping-2.2" mutable="true" name="MyApp.Domain.Model.Bar, MyApp.Domain, Version=3.5.0.0, Culture=neutral, PublicKeyToken=null" table="`Bar`">
        <id name="Id" type="System.Int32, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" unsaved-value="0">
            <column name="Id" />
            <generator class="identity" />
        </id>
        <property name="DateField" type="System.DateTime, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
            <column name="DateField" />
        </property>
        <property name="StringField" type="System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
            <column name="StringField" />
        </property>
        <bag cascade="save-update" inverse="true" name="Foos" mutable="true">
            <key foreign-key="FK_Foo_Bar" not-null="true">
                <column name="BarId" />
            </key>
            <one-to-many class="MyApp.Domain.Model.Foo, MyApp.Domain, Version=3.5.0.0, Culture=neutral, PublicKeyToken=null" />
        </bag>
    </class>
</hibernate-mapping>

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" default-access="property" auto-import="true" default-cascade="none" default-lazy="true">
    <class xmlns="urn:nhibernate-mapping-2.2" mutable="true" name="MyApp.Domain.Model.Foo, MyApp.Domain, Version=3.5.0.0, Culture=neutral, PublicKeyToken=null" table="`Foo`">
        <id name="Id" type="System.Int32, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" unsaved-value="0">
            <column name="Id" />
            <generator class="identity" />
        </id>
        <property name="DateField" type="System.DateTime, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
            <column name="DateField" />
        </property>
        <property name="BoolField" type="System.Boolean, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
            <column name="BoolField" />
        </property>
        <property name="StringField" type="System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
             <column name="StringField" />
        </property>
        <many-to-one class="MyApp.Domain.Model.Gorp, MyApp.Domain, Version=3.5.0.0, Culture=neutral, PublicKeyToken=null" name="Gorp">
            <column name="GorpId" />
        </many-to-one>
        <many-to-one class="MyApp.Domain.Model.Bar, MyApp.Domain, Version=3.5.0.0, Culture=neutral, PublicKeyToken=null" insert="false" name="Bar" update="false">
            <column name="BarId" not-null="true" />
        </many-to-one>
    </class>
</hibernate-mapping>

Exception:

could not insert: [MyApp.Domain.Model.Foo][SQL: INSERT INTO [Foo] (DateField, BoolField, StringField, GorpId, BarId) VALUES (?, ?, ?, ?, ?); select SCOPE_IDENTITY()]

What am I missing/misunderstanding?

  • 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-06-01T04:00:57+00:00Added an answer on June 1, 2026 at 4:00 am

    Thank you, Dave, for pointing me in the right direction. One of the Date fields was nullable in the database, the other was not. I changed my class and my map to reflect that. Voila! It worked!

    Too bad the original error message didn’t tell me there was an issue with a datetime field, it would’ve saved me some headaches.

    I made the following changes:

    public class Foo
    {
        public virtual int Id { get; set; }
        public virtual Bar Bar{ get; set; }
        public virtual DateTime DateField{ get; set; }
        public virtual string StringField{ get; set; }
        public virtual Gorp Gorp{ get; set; }
        public virtual bool BoolField{ get; set; }
    
        public Foo()
        {
            DateField = new DateTime(1753, 1, 1);
        }
    }
    
    public class Bar
    {
        public virtual int Id { get; set; }
        public virtual DateTime? DateField{ get; set; }
        public virtual string StringField{ get; set; }
    
        public virtual ICollection<Foo> Foos{ get; set; }
    
        public Bar()
        {
            DateField = new DateTime(1753, 1, 1);
            Foos= new List<Foo>();
        }
    
        public virtual void AddFoo(Foo foo)
        {
            foo.Bar = this;
            Foos.Add(foo);
        }
    }
    
    
    public class FooMap : ClassMap<Foo>
    {
        public FooMap()
        {
            Id(x => x.Id).UnsavedValue(0).GeneratedBy.Identity();
            Map(x => x.DateField);
            Map(x => x.BoolField);
            Map(x => x.StringField);
            References(x => x.Gorp)
              .Column("GorpId")
              .Class<Gorp>();
            References(x => x.Bar)
              .Column("BarId")
              .Not.Nullable();
        }
    }
    
    public class BarMap : ClassMap<Bar>
    {
        Id(x => x.Id).UnsavedValue(0).GeneratedBy.Identity();
        Map(x => x.DateField)
              .Nullable();
        Map(x => x.StringField);
    
        HasMany<Foo>(x => x.Foos)
          .AsBag()
          .Cascade.SaveUpdate()
          .Fetch.Join()
          .ForeignKeyConstraintName("FK_Foo_Bar")
          .Inverse()
          .KeyColumn("BarId")
          .Not.KeyNullable()
          .Table("Foo");
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a one to many relationship between two tables. The many table contains
I have a simple one-to-many relationship. I would like to select rows from the
I have a many-to-one relationship that users can edit via checkboxes. PK of Foo
Let's say we have one to many relationship between Customer and Phone.. class Customer{
I have a one to many relationship between tables A (columns ID and DESC
I have a one to many relationship between Admins and Users. Admin has_many :users
I have a one-to-many relationship between Part and Params (a Part has many Params).
I'm using Entity Framework 4 and have a one-to-many relationship between a parent and
I have a one to many relationship between a Course and Categories class Course
I have a one to many relationship between Games and Players. There is a

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.