Note that there are some explanatory texts on larger screens.

plurals
  1. POWhat is wrong with the following Fluent NHibernate Mapping?
    text
    copied!<p>I have 3 tables (Many to Many relationship)</p> <ol> <li>Resource {ResourceId, Description}</li> <li>Role {RoleId, Description}</li> <li>Permission {ResourceId, RoleId}</li> </ol> <p>I am trying to map above tables in fluent-nHibernate. This is what I am trying to do.</p> <pre><code>var aResource = session.Get&lt;Resource&gt;(1); // 2 Roles associated (Role 1 and 2) var aRole = session.Get&lt;Role&gt;(1); aResource.Remove(aRole); // I try to delete just 1 role from permission. </code></pre> <p><strong>But the sql generated here is (which is wrong)</strong></p> <pre><code>Delete from Permission where ResourceId = 1 Insert into Permission (ResourceId, RoleId) values (1, 2); </code></pre> <p><strong>Instead of (right way)</strong></p> <pre><code> Delete from Permission where ResourceId = 1 and RoleId = 1 </code></pre> <p>Why nHibernate behave like this? What wrong with the mapping? I even tried with Set instead of IList. Here is the full code.</p> <p><strong>Entities</strong></p> <pre><code>public class Resource { public virtual string Description { get; set; } public virtual int ResourceId { get; set; } public virtual IList&lt;Role&gt; Roles { get; set; } public Resource() { Roles = new List&lt;Role&gt;(); } } public class Role { public virtual string Description { get; set; } public virtual int RoleId { get; set; } public virtual IList&lt;Resource&gt; Resources { get; set; } public Role() { Resources = new List&lt;Resource&gt;(); } } </code></pre> <p><strong>Mapping Here</strong></p> <pre><code>// Mapping .. public class ResourceMap : ClassMap&lt;Resource&gt; { public ResourceMap() { Id(x =&gt; x.ResourceId); Map(x =&gt; x.Description); HasManyToMany(x =&gt; x.Roles).Table("Permission"); } } public class RoleMap : ClassMap&lt;Role&gt; { public RoleMap() { Id(x =&gt; x.RoleId); Map(x =&gt; x.Description); HasManyToMany(x =&gt; x.Resources).Table("Permission"); } } </code></pre> <p><strong>Program</strong></p> <pre><code> static void Main(string[] args) { var factory = CreateSessionFactory(); using (var session = factory.OpenSession()) { using (var tran = session.BeginTransaction()) { var aResource = session.Get&lt;Resource&gt;(1); var aRole = session.Get&lt;Role&gt;(1); aResource.Remove(aRole); session.Save(a); session.Flush(); tran.Commit(); } } } private static ISessionFactory CreateSessionFactory() { return Fluently.Configure() .Database(MsSqlConfiguration.MsSql2008 .ConnectionString("server=(local);database=Store;Integrated Security=SSPI")) .Mappings(m =&gt; m.FluentMappings.AddFromAssemblyOf&lt;Program&gt;() .Conventions.Add&lt;CustomForeignKeyConvention&gt;()) .BuildSessionFactory(); } public class CustomForeignKeyConvention : ForeignKeyConvention { protected override string GetKeyName(FluentNHibernate.Member property, Type type) { return property == null ? type.Name + "Id" : property.Name + "Id"; } } </code></pre> <p>Thanks, Ashraf.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload