CustomProfile不保存?

时间:2009-10-20 21:22:29

标签: c# asp.net asp.net-membership asp.net-profiles

我创建了一个扩展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);}
}

1 个答案:

答案 0 :(得分:1)

如果你看一下ProfileBase的实现,你会看到值的去/序列化和存储都在那里完成。它私下拥有一个包含属性信息及其序列化方式的集合。

迈克尔