奇怪的Guid.NewGuid()行为

时间:2012-04-02 18:48:33

标签: asp.net c#-4.0

我在网站上搜索了这个问题的答案,但我似乎无法想出这个问题。

我多次使用NewGuid()方法并且它有效。但现在由于某种原因它创造了一个空的Guid。

这是我的代码:

// Class of the Guid Object
public class CardUserAccount
    {
        // User ID of the user's profile
        public Guid UserId { get; set; }
    }

//Page object where method is called
Public partial class CreateSale : System.Web.UI.UserControl
    {
        // Create the UserProfile object
        public CardUserAccount profile = new CardUserAccount();


    protected void ContinueButton_Click(object sender, EventArgs e)
    {
        Guid _userId = Guid.NewGuid();
        profile.UserId = _userId;
    }

    protected void SubmitButton_Click(object sender, EventArgs e)
    {
        // Method to add object to database
        SubmitProfile(profile);
    }

然后我调用一个简单的linq to entities方法将对象添加到实体对象。

我已经仔细检查了它,我没有在任何地方覆盖它。

但是我在page_load方法之外创建配置文件对象可能是个问题。我认为这不会影响回发期间的对象。

我很感激帮助

1 个答案:

答案 0 :(得分:2)

这是实际代码吗?因为你声明并初始化变量,所以不做任何事情。

如果您打算覆盖字段值,则不应在此方法中声明该字段。