1

ProfileBaseを拡張するクラスを作成しました。

public class CustomerProfile : ProfileBase
{
    public int CustomerID { get; set; }


    public string CustomerName { get; set; }

    public static CustomerProfile GetProfile()
    {
        return Create(Membership.GetUser().UserName) as CustomerProfile;
    }

    public static CustomerProfile GetProfile(string userName)
    {
        return Create(userName) as CustomerProfile;
    }
}

私が行った場合:

CustomerProfile p = CustomerProfile.GetProfile();
p.CustomerID = 1;
p.Save();

次に現在のユーザーの値にアクセスしようとすると、1ではなく、0であるため、データベースに保存されていないようです。

私のweb.configファイルには、次のスニペットがあります。

<profile inherits="PortalBLL.CustomerProfile">
  <providers>
    <add name="CustomerProfile" type="System.Web.Profile.SqlProfileProvider" applicationName="/" connectionStringName="LocalSqlServer"/>
  </providers>
</profile>

次のことを試しましたが、うまくいきました。自動プロパティを使用して保存されないのはなぜですか。

[SettingsAllowAnonymous(false)]
public int CustomerID
{
    get { return (int)base.GetPropertyValue("CustomerID");}
    set { base.SetPropertyValue("CustomerID",value);}
}
4

1 に答える 1

1

ProfileBaseの実装を見ると、値の逆シリアル化と格納がそこで行われていることがわかります。プロパティ情報とそのシリアル化方法を含むコレクションを非公開で保持します。

マイケル

于 2010-03-11T05:39:11.450 に答える