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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T03:08:22+00:00 2026-05-26T03:08:22+00:00

I am pretty new to c#, EF Code First and all that, so my

  • 0

I am pretty new to c#, EF Code First and all that, so my question might be an easy one.

I am trying to create a very simple login. Each user must have a type (admin or client). How can I bind the usertype to my user table without generating a new type each time I insert a new user in the DB?
Here are my code first class:

public class User
{
    public int Id { get; set; }
    public string Username { get; set; }
    public string Password { get; set; }
    public bool Active { get; set; }
    public UserType TypeId { get; set; }
}

public class UserType
{
    public int Id { get; set; }
    public string Type { get; set; }
}

public enum TypeEnum
{
    Admin,
    Client
}

When I launch the app, I want to be able to create 2 tables:

  • Users which is empty
  • UserTypes which must contain 2 types (Admin and Client).

Then, everytime I register a new user, so everytime I add a user to the Users table, I want to be able to use the UserTypes table.

  • 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-26T03:08:23+00:00Added an answer on May 26, 2026 at 3:08 am

    When I launch the app, I want to be able to create 2 tables…

    Not sure if I understand this correctly but you can seed initial data into the database with EF Code-First. An example how to do that is here:

    Entity Framework Inserting Initial Data On Rebuild

    If you really want to recreate the tables with every launch of your application you can use the DropCreateDatabaseAlways<T> initializer as mentioned in the example.

    Then, everytime I register a new user, so everytime I add a user to
    the Users table, I want to be able to use the UserTypes table.

    For this you would load the existing UserType you want to assign from the database so that it is attached to the context and then create the new user:

    using (var context = new MyContext())
    {
        var userType = context.UserTypes
            .Single(u => u.Type == TypeEnum.Admin.ToString());
        var newUser = new User { TypeId = userType, Username = ... etc. };
    
        context.Users.Add(newUser);
    
        context.SaveChanges();
    }
    

    Attaching to the context – by either loading from the database or calling Attach explicitely – makes sure that the UserType is not duplicated, i.e. no INSERT command will be sent to the database for the UserType.

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

Sidebar

Related Questions

All, I'm pretty new to AS3, so this is probably a very trivial question.
I am pretty new at C# and .NET, but I've made this code to
I have this code: $coder = JSON::XS->new->utf8->pretty->allow_nonref; %perl = $coder->decode ($json); When I write
I'm pretty new to the idea of recursion and this is actually my first
I am pretty new to php, but I am learning! I have a simple
First off, this may be the wrong Forum for this question, as it's pretty
I'm pretty new to php, and for that matter server scripting in general (so
First of all, I'm kinda new to the barcode formats and what I do
I have a user input form here: http://www.7bks.com/create (Google login required) When you first
I am trying to write my first real python function that does something real.

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.