How to map specific row with NHibernate












0















I have the following situation:



// Model Attribute
int Id;
string Name;
int Classification;


and



// Model Role
int Id;
string Name;
List<Attribute> Attributes;


Database looks like this:



tbl_Role
Id Name
1 Admin
2 User


and



tbl_Attribute
Id Name Classification
1 Prename 1
2 Surname 2


and



tbl_Role_Attribute
RoleId ClassificationId
1 1
1 2
2 1


How can I create the mapping by code in NHibernate to get all allowed attributes for a role depending on the connected classification and resolve the attributes for JSON? How would the mapping for a model to the tbl_Role_Attribute look like? Something like:



public PolicyMapping()
{
Table("tbl_Role_Attribute");

ManyToOne(a => a.Role, b =>
{
b.Column($"RoleId");
});

ManyToOne(a => a.Attribute, b =>
{
b.Column($"ClassificationId");
});
}


Expected JSON output from pseudo getRole()



[{Id: 1,
Name: Admin,
allowedAttributes: {
Id: 1
Name: Prename
Classification: 1},
{Id: 2
Name: Surname
Classification: 2}}]


If you need further information let me know. Thanks in advance.



EDIT:



My target really is to map role to specific classifications (not every single attribute) and the get all attributes that are in that classification. (Imagine a classification like some sort of "group").










share|improve this question





























    0















    I have the following situation:



    // Model Attribute
    int Id;
    string Name;
    int Classification;


    and



    // Model Role
    int Id;
    string Name;
    List<Attribute> Attributes;


    Database looks like this:



    tbl_Role
    Id Name
    1 Admin
    2 User


    and



    tbl_Attribute
    Id Name Classification
    1 Prename 1
    2 Surname 2


    and



    tbl_Role_Attribute
    RoleId ClassificationId
    1 1
    1 2
    2 1


    How can I create the mapping by code in NHibernate to get all allowed attributes for a role depending on the connected classification and resolve the attributes for JSON? How would the mapping for a model to the tbl_Role_Attribute look like? Something like:



    public PolicyMapping()
    {
    Table("tbl_Role_Attribute");

    ManyToOne(a => a.Role, b =>
    {
    b.Column($"RoleId");
    });

    ManyToOne(a => a.Attribute, b =>
    {
    b.Column($"ClassificationId");
    });
    }


    Expected JSON output from pseudo getRole()



    [{Id: 1,
    Name: Admin,
    allowedAttributes: {
    Id: 1
    Name: Prename
    Classification: 1},
    {Id: 2
    Name: Surname
    Classification: 2}}]


    If you need further information let me know. Thanks in advance.



    EDIT:



    My target really is to map role to specific classifications (not every single attribute) and the get all attributes that are in that classification. (Imagine a classification like some sort of "group").










    share|improve this question



























      0












      0








      0








      I have the following situation:



      // Model Attribute
      int Id;
      string Name;
      int Classification;


      and



      // Model Role
      int Id;
      string Name;
      List<Attribute> Attributes;


      Database looks like this:



      tbl_Role
      Id Name
      1 Admin
      2 User


      and



      tbl_Attribute
      Id Name Classification
      1 Prename 1
      2 Surname 2


      and



      tbl_Role_Attribute
      RoleId ClassificationId
      1 1
      1 2
      2 1


      How can I create the mapping by code in NHibernate to get all allowed attributes for a role depending on the connected classification and resolve the attributes for JSON? How would the mapping for a model to the tbl_Role_Attribute look like? Something like:



      public PolicyMapping()
      {
      Table("tbl_Role_Attribute");

      ManyToOne(a => a.Role, b =>
      {
      b.Column($"RoleId");
      });

      ManyToOne(a => a.Attribute, b =>
      {
      b.Column($"ClassificationId");
      });
      }


      Expected JSON output from pseudo getRole()



      [{Id: 1,
      Name: Admin,
      allowedAttributes: {
      Id: 1
      Name: Prename
      Classification: 1},
      {Id: 2
      Name: Surname
      Classification: 2}}]


      If you need further information let me know. Thanks in advance.



      EDIT:



      My target really is to map role to specific classifications (not every single attribute) and the get all attributes that are in that classification. (Imagine a classification like some sort of "group").










      share|improve this question
















      I have the following situation:



      // Model Attribute
      int Id;
      string Name;
      int Classification;


      and



      // Model Role
      int Id;
      string Name;
      List<Attribute> Attributes;


      Database looks like this:



      tbl_Role
      Id Name
      1 Admin
      2 User


      and



      tbl_Attribute
      Id Name Classification
      1 Prename 1
      2 Surname 2


      and



      tbl_Role_Attribute
      RoleId ClassificationId
      1 1
      1 2
      2 1


      How can I create the mapping by code in NHibernate to get all allowed attributes for a role depending on the connected classification and resolve the attributes for JSON? How would the mapping for a model to the tbl_Role_Attribute look like? Something like:



      public PolicyMapping()
      {
      Table("tbl_Role_Attribute");

      ManyToOne(a => a.Role, b =>
      {
      b.Column($"RoleId");
      });

      ManyToOne(a => a.Attribute, b =>
      {
      b.Column($"ClassificationId");
      });
      }


      Expected JSON output from pseudo getRole()



      [{Id: 1,
      Name: Admin,
      allowedAttributes: {
      Id: 1
      Name: Prename
      Classification: 1},
      {Id: 2
      Name: Surname
      Classification: 2}}]


      If you need further information let me know. Thanks in advance.



      EDIT:



      My target really is to map role to specific classifications (not every single attribute) and the get all attributes that are in that classification. (Imagine a classification like some sort of "group").







      c# asp.net asp.net-core nhibernate






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 22 '18 at 11:11







      Raphael M.

















      asked Nov 21 '18 at 10:17









      Raphael M.Raphael M.

      15213




      15213
























          1 Answer
          1






          active

          oldest

          votes


















          0














          You would map Role and Attribute and then have a many-to-many on Role. Something like (assuming FluentNhibernate):



          public class AttributeMap : ClassMap<Attribute>
          {
          public AttributeMap()
          {
          Id(a => a.Id);
          Map(a => a.Name);
          Map(a => a.Classification);
          }
          }

          public class RoleMap : ClassMap<Role>
          {
          public RoleMap()
          {
          Id(r => r.Id);
          Map(r => r.Name);
          HasManyToMany(r => r.Attributes)
          }
          }


          Then you can create your JSON result like this:



          var results = 
          session
          .Query<Role>()
          .Fetch(r => r.Attributes)
          .ToList();

          var json =
          JsonConvert.SerializeObject(
          results
          .Select(r =>
          new
          {
          r.Id,
          r.Name,
          allowedAttributes = r.Attributes
          });





          share|improve this answer


























          • How would that mapping look like? Sorry I'm a little newby to this. Also I do not want an entry for every Role to Attribute in my tbl_Role_Attribute. That's one point of "grouping" them with classification

            – Raphael M.
            Nov 21 '18 at 11:23













          • Thank you very much for that input! But unfortunately that's not for my case, because let's say Role xy has access to Classification 1, 2 und 4. The query would only work with a foreach AND I would have to map every Attribute to every Role. Any other ideas?

            – Raphael M.
            Nov 22 '18 at 7:06













          • In the question, Role 1 has access to classification 1 & 2. Are you saying you want to constrain the classifications that it can have or find the roles that have certain classifications?

            – David Osborne
            Nov 22 '18 at 9:25











          • In your query in the .Where statement it gets only the attributes for classification 2. But as you said for Role 1 I would need those of classification 1&2

            – Raphael M.
            Nov 22 '18 at 11:14











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53409807%2fhow-to-map-specific-row-with-nhibernate%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          You would map Role and Attribute and then have a many-to-many on Role. Something like (assuming FluentNhibernate):



          public class AttributeMap : ClassMap<Attribute>
          {
          public AttributeMap()
          {
          Id(a => a.Id);
          Map(a => a.Name);
          Map(a => a.Classification);
          }
          }

          public class RoleMap : ClassMap<Role>
          {
          public RoleMap()
          {
          Id(r => r.Id);
          Map(r => r.Name);
          HasManyToMany(r => r.Attributes)
          }
          }


          Then you can create your JSON result like this:



          var results = 
          session
          .Query<Role>()
          .Fetch(r => r.Attributes)
          .ToList();

          var json =
          JsonConvert.SerializeObject(
          results
          .Select(r =>
          new
          {
          r.Id,
          r.Name,
          allowedAttributes = r.Attributes
          });





          share|improve this answer


























          • How would that mapping look like? Sorry I'm a little newby to this. Also I do not want an entry for every Role to Attribute in my tbl_Role_Attribute. That's one point of "grouping" them with classification

            – Raphael M.
            Nov 21 '18 at 11:23













          • Thank you very much for that input! But unfortunately that's not for my case, because let's say Role xy has access to Classification 1, 2 und 4. The query would only work with a foreach AND I would have to map every Attribute to every Role. Any other ideas?

            – Raphael M.
            Nov 22 '18 at 7:06













          • In the question, Role 1 has access to classification 1 & 2. Are you saying you want to constrain the classifications that it can have or find the roles that have certain classifications?

            – David Osborne
            Nov 22 '18 at 9:25











          • In your query in the .Where statement it gets only the attributes for classification 2. But as you said for Role 1 I would need those of classification 1&2

            – Raphael M.
            Nov 22 '18 at 11:14
















          0














          You would map Role and Attribute and then have a many-to-many on Role. Something like (assuming FluentNhibernate):



          public class AttributeMap : ClassMap<Attribute>
          {
          public AttributeMap()
          {
          Id(a => a.Id);
          Map(a => a.Name);
          Map(a => a.Classification);
          }
          }

          public class RoleMap : ClassMap<Role>
          {
          public RoleMap()
          {
          Id(r => r.Id);
          Map(r => r.Name);
          HasManyToMany(r => r.Attributes)
          }
          }


          Then you can create your JSON result like this:



          var results = 
          session
          .Query<Role>()
          .Fetch(r => r.Attributes)
          .ToList();

          var json =
          JsonConvert.SerializeObject(
          results
          .Select(r =>
          new
          {
          r.Id,
          r.Name,
          allowedAttributes = r.Attributes
          });





          share|improve this answer


























          • How would that mapping look like? Sorry I'm a little newby to this. Also I do not want an entry for every Role to Attribute in my tbl_Role_Attribute. That's one point of "grouping" them with classification

            – Raphael M.
            Nov 21 '18 at 11:23













          • Thank you very much for that input! But unfortunately that's not for my case, because let's say Role xy has access to Classification 1, 2 und 4. The query would only work with a foreach AND I would have to map every Attribute to every Role. Any other ideas?

            – Raphael M.
            Nov 22 '18 at 7:06













          • In the question, Role 1 has access to classification 1 & 2. Are you saying you want to constrain the classifications that it can have or find the roles that have certain classifications?

            – David Osborne
            Nov 22 '18 at 9:25











          • In your query in the .Where statement it gets only the attributes for classification 2. But as you said for Role 1 I would need those of classification 1&2

            – Raphael M.
            Nov 22 '18 at 11:14














          0












          0








          0







          You would map Role and Attribute and then have a many-to-many on Role. Something like (assuming FluentNhibernate):



          public class AttributeMap : ClassMap<Attribute>
          {
          public AttributeMap()
          {
          Id(a => a.Id);
          Map(a => a.Name);
          Map(a => a.Classification);
          }
          }

          public class RoleMap : ClassMap<Role>
          {
          public RoleMap()
          {
          Id(r => r.Id);
          Map(r => r.Name);
          HasManyToMany(r => r.Attributes)
          }
          }


          Then you can create your JSON result like this:



          var results = 
          session
          .Query<Role>()
          .Fetch(r => r.Attributes)
          .ToList();

          var json =
          JsonConvert.SerializeObject(
          results
          .Select(r =>
          new
          {
          r.Id,
          r.Name,
          allowedAttributes = r.Attributes
          });





          share|improve this answer















          You would map Role and Attribute and then have a many-to-many on Role. Something like (assuming FluentNhibernate):



          public class AttributeMap : ClassMap<Attribute>
          {
          public AttributeMap()
          {
          Id(a => a.Id);
          Map(a => a.Name);
          Map(a => a.Classification);
          }
          }

          public class RoleMap : ClassMap<Role>
          {
          public RoleMap()
          {
          Id(r => r.Id);
          Map(r => r.Name);
          HasManyToMany(r => r.Attributes)
          }
          }


          Then you can create your JSON result like this:



          var results = 
          session
          .Query<Role>()
          .Fetch(r => r.Attributes)
          .ToList();

          var json =
          JsonConvert.SerializeObject(
          results
          .Select(r =>
          new
          {
          r.Id,
          r.Name,
          allowedAttributes = r.Attributes
          });






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 22 '18 at 11:30

























          answered Nov 21 '18 at 11:12









          David OsborneDavid Osborne

          5,31911627




          5,31911627













          • How would that mapping look like? Sorry I'm a little newby to this. Also I do not want an entry for every Role to Attribute in my tbl_Role_Attribute. That's one point of "grouping" them with classification

            – Raphael M.
            Nov 21 '18 at 11:23













          • Thank you very much for that input! But unfortunately that's not for my case, because let's say Role xy has access to Classification 1, 2 und 4. The query would only work with a foreach AND I would have to map every Attribute to every Role. Any other ideas?

            – Raphael M.
            Nov 22 '18 at 7:06













          • In the question, Role 1 has access to classification 1 & 2. Are you saying you want to constrain the classifications that it can have or find the roles that have certain classifications?

            – David Osborne
            Nov 22 '18 at 9:25











          • In your query in the .Where statement it gets only the attributes for classification 2. But as you said for Role 1 I would need those of classification 1&2

            – Raphael M.
            Nov 22 '18 at 11:14



















          • How would that mapping look like? Sorry I'm a little newby to this. Also I do not want an entry for every Role to Attribute in my tbl_Role_Attribute. That's one point of "grouping" them with classification

            – Raphael M.
            Nov 21 '18 at 11:23













          • Thank you very much for that input! But unfortunately that's not for my case, because let's say Role xy has access to Classification 1, 2 und 4. The query would only work with a foreach AND I would have to map every Attribute to every Role. Any other ideas?

            – Raphael M.
            Nov 22 '18 at 7:06













          • In the question, Role 1 has access to classification 1 & 2. Are you saying you want to constrain the classifications that it can have or find the roles that have certain classifications?

            – David Osborne
            Nov 22 '18 at 9:25











          • In your query in the .Where statement it gets only the attributes for classification 2. But as you said for Role 1 I would need those of classification 1&2

            – Raphael M.
            Nov 22 '18 at 11:14

















          How would that mapping look like? Sorry I'm a little newby to this. Also I do not want an entry for every Role to Attribute in my tbl_Role_Attribute. That's one point of "grouping" them with classification

          – Raphael M.
          Nov 21 '18 at 11:23







          How would that mapping look like? Sorry I'm a little newby to this. Also I do not want an entry for every Role to Attribute in my tbl_Role_Attribute. That's one point of "grouping" them with classification

          – Raphael M.
          Nov 21 '18 at 11:23















          Thank you very much for that input! But unfortunately that's not for my case, because let's say Role xy has access to Classification 1, 2 und 4. The query would only work with a foreach AND I would have to map every Attribute to every Role. Any other ideas?

          – Raphael M.
          Nov 22 '18 at 7:06







          Thank you very much for that input! But unfortunately that's not for my case, because let's say Role xy has access to Classification 1, 2 und 4. The query would only work with a foreach AND I would have to map every Attribute to every Role. Any other ideas?

          – Raphael M.
          Nov 22 '18 at 7:06















          In the question, Role 1 has access to classification 1 & 2. Are you saying you want to constrain the classifications that it can have or find the roles that have certain classifications?

          – David Osborne
          Nov 22 '18 at 9:25





          In the question, Role 1 has access to classification 1 & 2. Are you saying you want to constrain the classifications that it can have or find the roles that have certain classifications?

          – David Osborne
          Nov 22 '18 at 9:25













          In your query in the .Where statement it gets only the attributes for classification 2. But as you said for Role 1 I would need those of classification 1&2

          – Raphael M.
          Nov 22 '18 at 11:14





          In your query in the .Where statement it gets only the attributes for classification 2. But as you said for Role 1 I would need those of classification 1&2

          – Raphael M.
          Nov 22 '18 at 11:14


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53409807%2fhow-to-map-specific-row-with-nhibernate%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          "Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

          Alcedinidae

          RAC Tourist Trophy