Cannot implicitly convert type 'string' to

2019-08-09 06:24发布

问题:

I keep getting these errors:

Error 1 Cannot implicitly convert type 'string' to 'NSWebSite.Models.Role'

public Role GetRoleForUser (User user)
{
    if (!UserExists(user))
        throw new ArgumentException(MissingUser);

    return user.Roles.TargetRoleName;
}

Error 2 'NSWebSite.Models.User' does not contain a definition for 'RoleID'

User newUser = new User()
{
    Name = name,
    Password = FormsAuthentication.HashPasswordForStoringInConfigFile(
        password.Trim(), "md5"),
    Email = email,
    RoleID = role.Id
};

Error 1 - I have no idea how to fix, so any advice is welcome.

Error 2 - I am getting it because my user model does not contain the definition for RoleID. If I want to set the role when I create a new user, what should I put here instead?

Below is my repository file (modified from here: brianleggDOTcom/post/2011/05/09/Implementing-your-own-RoleProvider-and-MembershipProvider-in-MVC-3.aspx) - DOT=. (dam antispam measures :-) )

http://www.mediafire.com/?gey4y9ub0v2u9nh

and my Model.Designer.cs file

http://www.mediafire.com/?qa3p9we8uqwfj09

回答1:

Your Error #2 simply means that your User class has no definition for a RoleID property. Look at the class definition and you can define it there.

Your Error #1 looks like you have a method with a return type of Role, and you're trying to do:

return user.Roles.TargetRoleName;  // this is a string???

If that string is what you do want to return, you'll need to modify your method like so:

string YourMethodName(...any parameters you have...)
{
    // ...your code here
    return SomeRole;
}

Or you can change your TargetRoleName property from type string to Role.



回答2:

My first question would be: Why is this all in the same class?

I would start by sorting the classes: Role, User, etc.

You need to define what a Role is and what a User is, separately.

public class User {

  private String name;,
  private String password;
  private String email;
  private RoleID role;

  public User(String name, String password, String email, RoleID role) {
    this.name = name;
    this.password = password;
    this.email = email;
    this.role = role;
  }

  public void someOtherMethod() {
    //some code here, etc
  }

}


回答3:

I am kinda new to this part of MVC and C#, from the original example at: https://www.brianlegg.com/post/2011/05/09/Implementing-your-own-RoleProvider-and-MembershipProvider-in-MVC-3.aspx

I had changed the DB schema from a 1 to many for user and roles to a many to many relationship. Changing the DB scheme back and updating the entity schema made everything work again like from the original blog post.

Why the original poster did not separate the two classes I am not sure, but once I get things working like I want I will start looking at cleaning up the code. If some one has any suggestions about how to make the example from the above mentioned webpage work with a many to many relationship for the User and Roles, they will be greatly appreciated :-)