Different User Types in ASP.NET Core 2.1 Using Composition





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















Today I explored creating different user types based on inheritance.



How to Define and use Different User Types in ASP.NET Core



After exploring solutions, I have learned that another approach is to use composition instead of inheritance.



The problem is that now when I run the initial migration, the following error is returned:




The child/dependent side could not be determined for the one-to-one relationship between 'Type1.User' and 'AppUser.Type1'. To identify the child/dependent side of the relationship, configure the foreign key property. If these navigations should not be part of the same relationship configure them without specifying the inverse.




Question: How can users of different types be created using the following composition?



AppUser.cs



using Microsoft.AspNetCore.Identity;

namespace MyApp.Models
{
public class AppUser : IdentityUser
{
public string FirstName { get; set; }
public string LastName { get; set; }
public Type1 Type1 { get; set; }
}
}


Type1.cs



namespace MyApp.Models
{
public class Type1
{
public int Type1Id { get; set; }
public string Property1 { get; set; }
public AppUser AppUser { get; set; }
}
}









share|improve this question

























  • you may have to stick some annotations to say which is the FK

    – JohnB
    Nov 23 '18 at 2:40











  • Could you provide an example?

    – crayden
    Nov 23 '18 at 2:42











  • [ForeignKey("YourFkId")]

    – JohnB
    Nov 23 '18 at 2:45











  • using System.ComponentModel.DataAnnotations.Schema;

    – JohnB
    Nov 23 '18 at 2:46











  • Thank you. I am not sure which property I should apply this to though.

    – crayden
    Nov 23 '18 at 2:47


















0















Today I explored creating different user types based on inheritance.



How to Define and use Different User Types in ASP.NET Core



After exploring solutions, I have learned that another approach is to use composition instead of inheritance.



The problem is that now when I run the initial migration, the following error is returned:




The child/dependent side could not be determined for the one-to-one relationship between 'Type1.User' and 'AppUser.Type1'. To identify the child/dependent side of the relationship, configure the foreign key property. If these navigations should not be part of the same relationship configure them without specifying the inverse.




Question: How can users of different types be created using the following composition?



AppUser.cs



using Microsoft.AspNetCore.Identity;

namespace MyApp.Models
{
public class AppUser : IdentityUser
{
public string FirstName { get; set; }
public string LastName { get; set; }
public Type1 Type1 { get; set; }
}
}


Type1.cs



namespace MyApp.Models
{
public class Type1
{
public int Type1Id { get; set; }
public string Property1 { get; set; }
public AppUser AppUser { get; set; }
}
}









share|improve this question

























  • you may have to stick some annotations to say which is the FK

    – JohnB
    Nov 23 '18 at 2:40











  • Could you provide an example?

    – crayden
    Nov 23 '18 at 2:42











  • [ForeignKey("YourFkId")]

    – JohnB
    Nov 23 '18 at 2:45











  • using System.ComponentModel.DataAnnotations.Schema;

    – JohnB
    Nov 23 '18 at 2:46











  • Thank you. I am not sure which property I should apply this to though.

    – crayden
    Nov 23 '18 at 2:47














0












0








0








Today I explored creating different user types based on inheritance.



How to Define and use Different User Types in ASP.NET Core



After exploring solutions, I have learned that another approach is to use composition instead of inheritance.



The problem is that now when I run the initial migration, the following error is returned:




The child/dependent side could not be determined for the one-to-one relationship between 'Type1.User' and 'AppUser.Type1'. To identify the child/dependent side of the relationship, configure the foreign key property. If these navigations should not be part of the same relationship configure them without specifying the inverse.




Question: How can users of different types be created using the following composition?



AppUser.cs



using Microsoft.AspNetCore.Identity;

namespace MyApp.Models
{
public class AppUser : IdentityUser
{
public string FirstName { get; set; }
public string LastName { get; set; }
public Type1 Type1 { get; set; }
}
}


Type1.cs



namespace MyApp.Models
{
public class Type1
{
public int Type1Id { get; set; }
public string Property1 { get; set; }
public AppUser AppUser { get; set; }
}
}









share|improve this question
















Today I explored creating different user types based on inheritance.



How to Define and use Different User Types in ASP.NET Core



After exploring solutions, I have learned that another approach is to use composition instead of inheritance.



The problem is that now when I run the initial migration, the following error is returned:




The child/dependent side could not be determined for the one-to-one relationship between 'Type1.User' and 'AppUser.Type1'. To identify the child/dependent side of the relationship, configure the foreign key property. If these navigations should not be part of the same relationship configure them without specifying the inverse.




Question: How can users of different types be created using the following composition?



AppUser.cs



using Microsoft.AspNetCore.Identity;

namespace MyApp.Models
{
public class AppUser : IdentityUser
{
public string FirstName { get; set; }
public string LastName { get; set; }
public Type1 Type1 { get; set; }
}
}


Type1.cs



namespace MyApp.Models
{
public class Type1
{
public int Type1Id { get; set; }
public string Property1 { get; set; }
public AppUser AppUser { get; set; }
}
}






c# asp.net-core asp.net-identity ef-migrations






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 23 '18 at 2:41









JohnB

2,02411420




2,02411420










asked Nov 23 '18 at 2:10









craydencrayden

4231622




4231622













  • you may have to stick some annotations to say which is the FK

    – JohnB
    Nov 23 '18 at 2:40











  • Could you provide an example?

    – crayden
    Nov 23 '18 at 2:42











  • [ForeignKey("YourFkId")]

    – JohnB
    Nov 23 '18 at 2:45











  • using System.ComponentModel.DataAnnotations.Schema;

    – JohnB
    Nov 23 '18 at 2:46











  • Thank you. I am not sure which property I should apply this to though.

    – crayden
    Nov 23 '18 at 2:47



















  • you may have to stick some annotations to say which is the FK

    – JohnB
    Nov 23 '18 at 2:40











  • Could you provide an example?

    – crayden
    Nov 23 '18 at 2:42











  • [ForeignKey("YourFkId")]

    – JohnB
    Nov 23 '18 at 2:45











  • using System.ComponentModel.DataAnnotations.Schema;

    – JohnB
    Nov 23 '18 at 2:46











  • Thank you. I am not sure which property I should apply this to though.

    – crayden
    Nov 23 '18 at 2:47

















you may have to stick some annotations to say which is the FK

– JohnB
Nov 23 '18 at 2:40





you may have to stick some annotations to say which is the FK

– JohnB
Nov 23 '18 at 2:40













Could you provide an example?

– crayden
Nov 23 '18 at 2:42





Could you provide an example?

– crayden
Nov 23 '18 at 2:42













[ForeignKey("YourFkId")]

– JohnB
Nov 23 '18 at 2:45





[ForeignKey("YourFkId")]

– JohnB
Nov 23 '18 at 2:45













using System.ComponentModel.DataAnnotations.Schema;

– JohnB
Nov 23 '18 at 2:46





using System.ComponentModel.DataAnnotations.Schema;

– JohnB
Nov 23 '18 at 2:46













Thank you. I am not sure which property I should apply this to though.

– crayden
Nov 23 '18 at 2:47





Thank you. I am not sure which property I should apply this to though.

– crayden
Nov 23 '18 at 2:47












1 Answer
1






active

oldest

votes


















0














You could refer to EF core relationships to set one to one relationship.



Change Type1.cs to below code, then add migrations.



public class Type1
{
public int Type1Id { get; set; }
public string Property1 { get; set; }

public string AppUserId {get;set;}//As foreign Key
public AppUser AppUser { get; set; }
}


Create user with specific Type in Register.cshtml.cs.



var user = new AppUser {
UserName = Input.Email,
Email = Input.Email,
Type1= new Type1{ Property1="Admin"}
};
var result = await _userManager.CreateAsync(user, Input.Password);





share|improve this answer
























    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%2f53439884%2fdifferent-user-types-in-asp-net-core-2-1-using-composition%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 could refer to EF core relationships to set one to one relationship.



    Change Type1.cs to below code, then add migrations.



    public class Type1
    {
    public int Type1Id { get; set; }
    public string Property1 { get; set; }

    public string AppUserId {get;set;}//As foreign Key
    public AppUser AppUser { get; set; }
    }


    Create user with specific Type in Register.cshtml.cs.



    var user = new AppUser {
    UserName = Input.Email,
    Email = Input.Email,
    Type1= new Type1{ Property1="Admin"}
    };
    var result = await _userManager.CreateAsync(user, Input.Password);





    share|improve this answer




























      0














      You could refer to EF core relationships to set one to one relationship.



      Change Type1.cs to below code, then add migrations.



      public class Type1
      {
      public int Type1Id { get; set; }
      public string Property1 { get; set; }

      public string AppUserId {get;set;}//As foreign Key
      public AppUser AppUser { get; set; }
      }


      Create user with specific Type in Register.cshtml.cs.



      var user = new AppUser {
      UserName = Input.Email,
      Email = Input.Email,
      Type1= new Type1{ Property1="Admin"}
      };
      var result = await _userManager.CreateAsync(user, Input.Password);





      share|improve this answer


























        0












        0








        0







        You could refer to EF core relationships to set one to one relationship.



        Change Type1.cs to below code, then add migrations.



        public class Type1
        {
        public int Type1Id { get; set; }
        public string Property1 { get; set; }

        public string AppUserId {get;set;}//As foreign Key
        public AppUser AppUser { get; set; }
        }


        Create user with specific Type in Register.cshtml.cs.



        var user = new AppUser {
        UserName = Input.Email,
        Email = Input.Email,
        Type1= new Type1{ Property1="Admin"}
        };
        var result = await _userManager.CreateAsync(user, Input.Password);





        share|improve this answer













        You could refer to EF core relationships to set one to one relationship.



        Change Type1.cs to below code, then add migrations.



        public class Type1
        {
        public int Type1Id { get; set; }
        public string Property1 { get; set; }

        public string AppUserId {get;set;}//As foreign Key
        public AppUser AppUser { get; set; }
        }


        Create user with specific Type in Register.cshtml.cs.



        var user = new AppUser {
        UserName = Input.Email,
        Email = Input.Email,
        Type1= new Type1{ Property1="Admin"}
        };
        var result = await _userManager.CreateAsync(user, Input.Password);






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 23 '18 at 8:43









        Xing ZouXing Zou

        1,282117




        1,282117
































            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%2f53439884%2fdifferent-user-types-in-asp-net-core-2-1-using-composition%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

            mysqli_query(): Empty query in /home/lucindabrummitt/public_html/blog/wp-includes/wp-db.php on line 1924

            How to change which sound is reproduced for terminal bell?

            Can I use Tabulator js library in my java Spring + Thymeleaf project?