简体   繁体   English

asp.net用户配置文件:数据库拥有用户后,如何添加字段?

[英]asp.net User Profile: how do I add a field once the DB has Users?

My App has been live for some time now so there are many users in the db and they already have profile data attached, I now need to add an extra field to the profile. 我的应用程序已经启用了一段时间,因此数据库中有许多用户,他们已经附加了配置文件数据,现在我需要在配置文件中添加一个额外的字段。

I added the new field to my web.config file: 我将新字段添加到我的web.config文件中:

<add name="DefaultToOK" type="System.Boolean" defaultValue="True" />

I updated all the relevant classes and methods to assign the variable: 我更新了所有相关的类和方法来分配变量:

// RegistrationData class    
...
[Display(Name = "DefaultToOKLabel", Description = "DefaultToOKDescription", ResourceType = typeof(RegistrationDataResources))]
public bool DefaultToOK { get; set; }
...

public void GetDataFromMembershipUser(MembershipUser user)
{
    WebProfile profile = WebProfile.GetProfile(user.UserName);

    ...
    if (profile.DefaultToOK != null)
        this.DefaultToOK = profile.DefaultToOK;
    ...
}

// similarly added elsewhere (User.cs, UserRegistrationService.cs and UserData POCO)

When I try to run and login, it comes back with an exception (understandably) as the field is not found in the profile data in the db: 当我尝试运行和登录时,它返回一个异常(可以理解),因为在数据库的配置文件数据中找不到该字段:

System.ServiceModel.DomainServices.Client.DomainOperationException was unhandled by user code
  Message=Load operation failed for query 'Login'. 
  A profile property does not exist for DefaultsToOK. 
  InnerException message: The settings property 'DefaultsToOK' was not found.
  ErrorCode=500 
  ...

My question is: how do I add a new field to the asp_net profile without having an exception thrown? 我的问题是:如何在不引发异常的情况下向asp_net配置文件添加新字段?

EDIT: Web config 编辑:Web配置

<profile defaultProvider="AspNetSqlProfileProvider" enabled="true">
  <providers>
    <clear />
    <add name="AspNetSqlProfileProvider" type="MyApp.Web.MySqlProfileProvider" connectionStringName="ApplicationServices" applicationName="/" />
  </providers>
  <properties>
    <add name="FriendlyName" />
    <add name="JobTitle" />
    <add name="TopLevelViewName" defaultValue="[ANY]" />
    <add name="TopLevelViewID" type="System.Guid" defaultValue="[null]" />
    <add name="RouteIDs" />
    <add name="TopLevelViewType" type="System.Int32" defaultValue="0" />
    <add name="PasswordQuestionAnswer" />
    <add name="Criticality" type="System.Int32" defaultValue="0" />
    <!-->add name="DefaultToOK" type="System.Boolean" defaultValue="True" /-->
  </properties>
</profile>

Web Profile 网站资料

public partial class WebProfile {

        private System.Web.Profile.ProfileBase _profileBase;

        public WebProfile() {
            this._profileBase = new System.Web.Profile.ProfileBase();
        }

        public WebProfile(System.Web.Profile.ProfileBase profileBase) {
            this._profileBase = profileBase;
        }

        public virtual string RouteIDs {
            get {
                return ((string)(this.GetPropertyValue("RouteIDs")));
            }
            set {
                this.SetPropertyValue("RouteIDs", value);
            }
        }

        public virtual string PasswordQuestionAnswer {
            get {
                return ((string)(this.GetPropertyValue("PasswordQuestionAnswer")));
            }
            set {
                this.SetPropertyValue("PasswordQuestionAnswer", value);
            }
        }

        public virtual string JobTitle {
            get {
                return ((string)(this.GetPropertyValue("JobTitle")));
            }
            set {
                this.SetPropertyValue("JobTitle", value);
            }
        }

        public virtual int TopLevelViewType {
            get {
                return ((int)(this.GetPropertyValue("TopLevelViewType")));
            }
            set {
                this.SetPropertyValue("TopLevelViewType", value);
            }
        }

        public virtual string FriendlyName {
            get {
                return ((string)(this.GetPropertyValue("FriendlyName")));
            }
            set {
                this.SetPropertyValue("FriendlyName", value);
            }
        }

        public virtual string TopLevelViewName {
            get {
                return ((string)(this.GetPropertyValue("TopLevelViewName")));
            }
            set {
                this.SetPropertyValue("TopLevelViewName", value);
            }
        }

        public virtual int Criticality {
            get {
                return ((int)(this.GetPropertyValue("Criticality")));
            }
            set {
                this.SetPropertyValue("Criticality", value);
            }
        }

        public virtual System.Guid TopLevelViewID {
            get {
                return ((System.Guid)(this.GetPropertyValue("TopLevelViewID")));
            }
            set {
                this.SetPropertyValue("TopLevelViewID", value);
            }
        }

        public static WebProfile Current {
            get {
                return new WebProfile(System.Web.HttpContext.Current.Profile);
            }
        }

        public virtual System.Web.Profile.ProfileBase ProfileBase {
            get {
                return this._profileBase;
            }
        }

        public virtual object this[string propertyName] {
            get {
                return this._profileBase[propertyName];
            }
            set {
                this._profileBase[propertyName] = value;
            }
        }

        public virtual string UserName {
            get {
                return this._profileBase.UserName;
            }
        }

        public virtual bool IsAnonymous {
            get {
                return this._profileBase.IsAnonymous;
            }
        }

        public virtual bool IsDirty {
            get {
                return this._profileBase.IsDirty;
            }
        }

        public virtual System.DateTime LastActivityDate {
            get {
                return this._profileBase.LastActivityDate;
            }
        }

        public virtual System.DateTime LastUpdatedDate {
            get {
                return this._profileBase.LastUpdatedDate;
            }
        }

        public virtual System.Configuration.SettingsProviderCollection Providers {
            get {
                return this._profileBase.Providers;
            }
        }

        public virtual System.Configuration.SettingsPropertyValueCollection PropertyValues {
            get {
                return this._profileBase.PropertyValues;
            }
        }

        public virtual System.Configuration.SettingsContext Context {
            get {
                return this._profileBase.Context;
            }
        }

        public virtual bool IsSynchronized {
            get {
                return this._profileBase.IsSynchronized;
            }
        }

        public static System.Configuration.SettingsPropertyCollection Properties {
            get {
                return System.Web.Profile.ProfileBase.Properties;
            }
        }

        public static WebProfile GetProfile(string username) {
            return new WebProfile(System.Web.Profile.ProfileBase.Create(username));
        }

        public static WebProfile GetProfile(string username, bool authenticated) {
            return new WebProfile(System.Web.Profile.ProfileBase.Create(username, authenticated));
        }

        public virtual object GetPropertyValue(string propertyName) {
            return this._profileBase.GetPropertyValue(propertyName);
        }

        public virtual void SetPropertyValue(string propertyName, object propertyValue) {
            this._profileBase.SetPropertyValue(propertyName, propertyValue);
        }

        public virtual System.Web.Profile.ProfileGroupBase GetProfileGroup(string groupName) {
            return this._profileBase.GetProfileGroup(groupName);
        }

        public virtual void Initialize(string username, bool isAuthenticated) {
            this._profileBase.Initialize(username, isAuthenticated);
        }

        public virtual void Save() {
            this._profileBase.Save();
        }

        public virtual void Initialize(System.Configuration.SettingsContext context, System.Configuration.SettingsPropertyCollection properties, System.Configuration.SettingsProviderCollection providers) {
            this._profileBase.Initialize(context, properties, providers);
        }

        public static System.Configuration.SettingsBase Synchronized(System.Configuration.SettingsBase settingsBase) {
            return System.Web.Profile.ProfileBase.Synchronized(settingsBase);
        }

        public static System.Web.Profile.ProfileBase Create(string userName) {
            return System.Web.Profile.ProfileBase.Create(userName);
        }

        public static System.Web.Profile.ProfileBase Create(string userName, bool isAuthenticated) {
            return System.Web.Profile.ProfileBase.Create(userName, isAuthenticated);
        }
    }

If you load your profile programmatically then modify the code below to set your values I believe it should do the trick: 如果您以编程方式加载个人资料,请修改下面的代码以设置您的值,我相信它应该可以解决问题:

var profile = ProfileBase.Create(username);
profile.SetPropertyValue("MyGuid", aGuid);
profile.SetPropertyValue("MyString", aString);
// etc
profile.Save()

ASP.NET Profiles - Add a profile to an existing user ASP.NET配置文件-将配置文件添加到现有用户

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将ASP.NET成员资格/角色用户链接到db中的表? - How do I link ASP.NET membership/role users to tables in db? 如何在ASP.NET(3.5)中创建“公共”用户配置文件页面 - How do I create a “public” user profile page in ASP.NET (3.5) 在ASP.NET Core 2.1中,如何在用户基于角色登录后添加菜单项? - In ASP.NET Core 2.1 how do I add menu items after the user has logged in based on Role? 一旦用户通过Asp.Net中的成员资格和角色创建新帐户,我该如何进行身份验证 - How do i authenticate once the user create a new Account by Membership & Roles in Asp.Net 如何将用户字段设置为当前记录的用户ASP.NET MVC的数据库记录添加 - How should I add a database record with user field set to currently logged user ASP.NET MVC 如何将用户配置文件图片添加到Asp.net成员资格(aspnet_Profile)表中? - How to add user profile picture to Asp.net Membership (aspnet_Profile) table? 如何在数据库中创建来自用户系统中ASP.NET的对用户的引用? - How do I create a reference in my DB to a user from the ASP.NET build in usersystem? 如何在ASP.NET MVC 4实体框架中仅将特定的数据库实体集添加到ViewData? - How do I add ONLY specific DB Entity Sets to the ViewData in ASP.NET MVC 4 Entity Framework? 如果字段值已无效,如何将其保留在ASP.NET MVC表单中? - How do I keep field values in the ASP.NET MVC form if it has been invalidated? ASP.NET配置文件 - 向现有用户添加配置文件 - ASP.NET Profiles - Add a profile to an existing user
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM