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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T19:49:30+00:00 2026-05-23T19:49:30+00:00

I’ve a layer name MVCTemplate.Common, having two folders Entities and Mappings. In Entities User.cs

  • 0

I’ve a layer name MVCTemplate.Common, having two folders Entities and Mappings. In Entities User.cs and Role.cs is as under respectively:

using System;
using System.Collections.Generic;

namespace MVCTemplate.Common.Entities
{
    public class User
    {
        public virtual Guid UserID { get; set; }
        public virtual string UserName { get; set; }
        public virtual string Password { get; set; }
        public virtual string FullName { get; set; }
        public virtual string Email { get; set; }
        public virtual TimeSpan LastLogin { get; set; }
        public virtual bool IsActive { get; set; }
        public virtual DateTime CreationDate { get; set; }
        public virtual IList<Role> UserInRoles { get; set; }

        public User()
        {
            UserInRoles = new List<Role>();
        }
    }
}

using System.Collections.Generic;

namespace MVCTemplate.Common.Entities
{
    public class Role
    {
        public virtual int? RoleID { get; set; }
        public virtual string RoleName { get; set; }
        public virtual bool IsActive { get; set; }
        public virtual string Description { get; set; }
        public virtual IList<User> Users { get; set; }
        //public virtual IList<RolePermission> RolePermission { get; set; }

        public Role()
        {
            Users = new List<User>();
        }

        public virtual void AddUsers(User _user)
        {
            _user.UserInRoles.Add(this);
            Users.Add(_user);
        }
    }
}

Now, In Mappings folder their mappings files is as under:

using FluentNHibernate.Mapping;
using MVCTemplate.Common.Entities;

namespace MVCTemplate.Common.Mappings
{
   public class RoleMap : ClassMap<Role>
    {
        public RoleMap()
        {
            Table("tblRoles");
            Id(role => role.RoleID).GeneratedBy.Identity();
            Map(role => role.RoleName).Not.Nullable();
            Map(role => role.IsActive).Not.Nullable();
            Map(role => role.Description).Not.Nullable();
            HasManyToMany(role => role.Users).Cascade.All().Table("tblUserInRoles");
            //HasMany(role => role.User);
            //HasMany(role => role.RolePermission);
        }
    }
}

using FluentNHibernate.Mapping;
using MVCTemplate.Common.Entities;

namespace MVCTemplate.Common.Mappings
{
    public class UserMap : ClassMap<User>
    {
        public UserMap()
        {
            Table("tblUsers");
            Id(user => user.UserID).GeneratedBy.Guid();
            Map(user => user.UserName).Not.Nullable();
            Map(user => user.Password).Not.Nullable();
            Map(user => user.FullName).Not.Nullable();
            Map(user => user.Email).Not.Nullable();
            //Map(user => user.LastLogin).Nullable();
            Map(user => user.IsActive).Not.Nullable();
            Map(user => user.CreationDate).Not.Nullable();
            HasManyToMany(user => user.UserInRoles).Cascade.All().Table("tblUserInRoles");
            //HasMany(user => user.UserInRoles);
        }
    }
}

Now, from controller, I got specific user object in user object and now want to delete it, the simple code is as under:

using System;
using System.Collections.Generic;
using System.Web.Mvc;
using MVCTemplate.BussinessLayer.Facade;
using MVCTemplate.Common.Entities;

namespace MVCTemplate.Web.Controllers
{
    public class HomeController : Controller
    {

        private UserManagement _userManagement;
        public ActionResult Index()
        {
            ViewBag.Message = "Welcome to ASP.NET MVC!";

            _userManagement = new UserManagement();
            var oUser = _userManagement.GetUserBy(new Guid("4fb5856a-58d9-4b78-8d08-bce645bc93c7"));

            oUser.UserInRoles = new List<Role>();
            _userManagement.Delete(oUser);

            return View();
        }

        public ActionResult About()
        {
            return View();
        }
    }
}

Now, I just want to delete that user as this user is not exist in any role and there is no entry of it in its junction table(UserInRole). I initialize its Role collection to empty for Count=0, but after calling deleting method it throw an error with sqlQuery which is as under:

could not delete collection: [MVCTemplate.Common.Entities.User.UserInRoles#4fb5856a-58d9-4b78-8d08-bce645bc93c7][SQL: DELETE FROM tblUserInRoles WHERE User_id = @p0]

Note that in Junction table tblUserInRole there no field of that name User_id as mentioned in error, due to which in the inner exception it is telling that column name User_id not exist. But in junction table I have only two field 1) UserID and 2) RoleID.

Any suggestion how to resolve it?

  • 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-23T19:49:32+00:00Added an answer on May 23, 2026 at 7:49 pm

    looks like a mapping error to me. Change:

    HasManyToMany(user => user.UserInRoles).Cascade.All().Table("tblUserInRoles");
    

    To

    HasManyToMany(user => user.UserInRoles)
        .Cascade.All()
        .Table("tblUserInRoles")
        .ParentKeyColumn("UserID")
        .ChildKeyColumn("RoleID");
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
We're building an app, our first using Rails 3, and we're having to build
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need to clean up various Word 'smart' characters in user input, including but
We are using XSLT to translate a RIXML file to XML. Our RIXML contains

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.