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

  • Home
  • SEARCH
  • 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 8174369
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T22:26:29+00:00 2026-06-06T22:26:29+00:00

I have just started implementing ISet ‘s instead of IList ‘s in my project

  • 0

I have just started implementing ISet‘s instead of IList‘s in my project as that better reflects the collection’s I have.

This has thrown up an odd error in the creation of one table. The error I’m getting is.

Cannot define PRIMARY KEY constraint on nullable column in table ‘UserAddressAssignments’.

I have a many to many relationship between users and addresses.

public class User : BaseEntity
{
    public virtual System.Guid UserGuid { get; set; }
    public virtual string UserName { get; set; }
    public virtual Company Company { get; set; }
    public virtual Iesi.Collections.Generic.ISet<Address> UserAddressAssignments { get; set; }
}

The SQL it is generating for the lookup column is causing the issue and is shown below. It is the UserId column being nullable which is causing the problem.

create table UserAddressAssignments (
    UserAddressAssignmentId INT not null,
   AddressId INT IDENTITY NOT NULL,
   UserId INT null,
   primary key (UserId, AddressId)
)

The odd part here is that I have an identical relationship between a company and addresses which works fine.

public class Company : BaseEntity
{
    public virtual System.Guid CompanyGuid { get; set; }
    public virtual string Name { get; set; }
    public virtual Iesi.Collections.Generic.ISet<Address> CompanyAddressAssignments { get; set; }
}

This create the correct SQL for the lookup table.

CREATE TABLE [dbo].[CompanyAddressAssignments](
    [CompanyId] [int] NOT NULL,
    [AddressId] [int] NOT NULL,
PRIMARY KEY CLUSTERED 
(
    [CompanyId] ASC,
    [AddressId] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

With a clustered key with the correct not null attributes for the keys and no third ‘CompanyAddressAssignmentId’ column as with the users lookup column.

The mapping overrides for both the Company and the User are identical too.

public class CompanyMappingOverride : IAutoMappingOverride<Company>
{
        public void Override(FluentNHibernate.Automapping.AutoMapping<Company> mapping)
        {
            mapping.Map(x => x.Name);
            mapping.HasManyToMany(x => x.CompanyAddressAssignments).Table("CompanyAddressAssignments").Cascade.All();
        }
}

public class UserMappingOverride : IAutoMappingOverride<User>
{
    public void Override(FluentNHibernate.Automapping.AutoMapping<User> mapping)
    {
        mapping.References(x => x.Company).Cascade.All();
        mapping.HasManyToMany(x => x.UserAddressAssignments).Table("UserAddressAssignments").Cascade.All();
    }
}

Does anyone have any suggestions on why the two lookup tables look so different when as far as I can tell the structure and mappings of the classes are identical?

I have resorted to using an IList for user addresses now to work around the issue.

This issues this create sql.

CREATE TABLE [dbo].[CompanyAddressAssignments](
    [CompanyId] [int] NOT NULL,
    [AddressId] [int] NOT NULL,
PRIMARY KEY CLUSTERED 
(
    [CompanyId] ASC,
    [AddressId] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

Any ideas on what could be causing this discrepancy?

NHibernate version is 3.2.0.4000.

Cheers

Steve

  • 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-06T22:26:30+00:00Added an answer on June 6, 2026 at 10:26 pm

    Right I’ve found out what the issue was.

    The site I’m working on has had NHibernate added to it. In order to use the code first / schema generation approach I used an automated tool to convert the existing schema into classes which the schema could then be magically created from.

    I used NHhibernate designer to auto-generated the classes and then cleaned up the entities to match our standards.

    http://www.devart.com/entitydeveloper/nhibernate-designer.html

    The error crept in as the tool generated a class for the lookup table. So the UserAddressAssignment has its own class in the system. When creating a many to many relationship this is not usually required. Having specified both a class for the many to many and ALSO in the mapping override given the name of a ManyToMany table NHibernate must have got confused (unsurprisingly).

    I would like to point out that the schema that NHibernate Designer had to work with was not all that clean any some of the relationships where not enforced, so I am not blaming it for creating this entity when not required!

    So when I removed the unnecessary UserAddressAssignment class given below everything worked as expected.

      public class UserAddressAssignment : BaseEntity
        {
            public virtual int AddressId { get; set; }
            public virtual User User { get; set; }
            public virtual Address Address { get; set; }
        }
    

    Hope this helps some one!

    Cheers

    Steve

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

Sidebar

Related Questions

I have just started implementing signal listeners in a django project. While I understand
we have just started using a git account of our Django website project so
I have just started reading on implementing RESTful web services and creating RESTful apis.
I have just started a new project using a Linq to Sql model and
I have been looking at using TDD and implementing proper testing (only just started
I've just started developing for Android. and i have some problems implementing an SQLite
I've just started using Git and find that whilst I am implementing a feature
I have just started using RSpec and I copied the very simple test on
I have just started leaning and working on xquery with java. I have a
I have just started learning python version 3 and trying to create a file

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.