无法将类型'string'隐式转换为Model [英] Cannot implicitly convert type 'string' to Model

查看:459
本文介绍了无法将类型'string'隐式转换为Model的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不断收到这些错误:

错误1无法将类型'string'隐式转换为'NSWebSite.Models.Role'

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;
}

错误2'NSWebSite.Models.User'不包含'RoleID'的定义

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
};

错误1 -我不知道如何解决,因此欢迎您提出任何建议.

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

错误2 -我得到它是因为我的用户模型不包含RoleID的定义.如果要在创建新用户时设置角色,该怎么办?

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?

下面是我的存储库文件(从此处修改:brianleggDOTcom/post/2011/05/09/Implementing-your-own-RoleProvider-and-MembershipProvider-in-MVC-3.aspx)-DOT =. (大坝反垃圾邮件措施:-))

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

和我的Model.Designer.cs文件

and my Model.Designer.cs file

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

推荐答案

对于MVC和C#的这一部分,我有点陌生,其原始示例位于:

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

我已将数据库模式从1更改为多个,将用户和角色的关系更改为多对多的关系.改回数据库方案并更新实体架构,使得一切都可以正常工作,就像原始博客文章中一样.

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 :-)

这篇关于无法将类型'string'隐式转换为Model的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆