2013-05-10 17 views
2

我的應用程序已經運行了一段時間,所以db中有很多用戶,並且他們已經有了配置文件數據,現在我需要爲配置文件添加一個額外的字段。asp.net用戶配置文件:如何在數據庫擁有用戶後添加字段?

我加入了新的領域,以我的web.config文件:

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

我更新所有相關的類和方法給變量賦值:

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

當我嘗試運行和登錄,它回來了一個例外(可以理解),因爲該字段中沒有發現數據庫中的配置文件數據:

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 
    ... 

我的問題是:如何添加一個新的字段到asp_net配置文件而不會引發異常?

編輯: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); 
     } 
    } 
+0

你可以顯示你的WebProfile類和web.config嗎? – 2013-08-08 05:21:47

+0

添加到問題 – xhedgepigx 2013-08-09 10:49:42

回答

0

如果加載您的配置文件編程然後修改下面的代碼來設置你的價值觀,我相信它應該做的伎倆:

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

相關問題