1

So I have two tables in my db, engineer and engineerSettings. I currently have a controller for both api/username and api/userSettings, but I also want to have a controller api/userInformation that returns everything in username as well as userSettings. I'm not really sure how to go about doing this, but right now I have a new model for userInformation

public class userInformation
{
    public virtual ICollection<Username> Usernames {get;set;}
    punlic virtual ICollection<UserSetting> UserSettings {get;set;}
}

And in my UserInformation controller I have

.... 
//GET
public IEnumerable<UserInformation> GetUserInformation()
{
    return db.UserInformations;
}

Which doesn't really work so I was wondering how to solve this problem?

7
  • What isn't working? Commented Oct 17, 2016 at 22:26
  • I'm getting an entity type UserInformation is not part of the model for the current context error, and I have tried adding modelBuilder.Entity<UserInformation>().ToTable("UserInformation") to my dbcontext, but still getting the same error. Commented Oct 17, 2016 at 22:29
  • Look in your context class. It has to be defined to use it. Commented Oct 17, 2016 at 22:30
  • Yes I already defined it, public virtual DbSet<UserSetting> UserSettings { get; set; } Commented Oct 17, 2016 at 22:35
  • 2
    FWIW, Visual Studio (if that's what you're using) offers a couple of straight-forward methods to troubleshoot your code. They've been very helpful in finding solutions to questions not found in SO. The first one involves right-clicking on the far left of the code line for the method that's not working, and running it in local debug, your VS needs to be configured for symbols. The second method involves creating a .TESTS project, referencing your main project and adding the methods as [TestMethod] you will sometimes get much more detailed error info then on your browser. HTH. Commented Oct 17, 2016 at 22:47

1 Answer 1

4

Fill your model:

public UserInformation GetUserInformation()
{
        UserInformation UserInfo= new UserInformation();
        List<Username> lstUsername= new List<Username>();
        List<UserSetting> lstUserSetting= new List<UserSetting>();
        lstUserSetting=db.UserSettings.ToList();
        lstUsername=db.Usernames.ToList();
        UserInfo.Usernames =lstUsername;
        UserInfo.UserSettings =lstUserSetting;
        return UserInfo;
}
Sign up to request clarification or add additional context in comments.

3 Comments

Yess. Thank you! I knew it wasn't as straightforward as I thought. :)
How would I say, if Usernames and usersetting both share an "Id", how do i only return the usernames and usersetting of that Id?
for selecting single user with userID db.Usernames.Where(m => m.ID == userID).FirstOrDefault();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.