开发者

How to test if my linq query went through

This is my code from my controller:

MGEntities db = new MGEntities();
 [HttpPost]
    public ActionResult Register(RegisterModel model)
    {
        if (ModelState.IsValid)
    开发者_如何转开发    {
            // Attempt to register the user
            MembershipCreateStatus createStatus = MembershipService.CreateUser(model.UserName, model.Password, model.Email);

            if (createStatus == MembershipCreateStatus.Success)
            {

                FormsService.SignIn(model.UserName, false /* createPersistentCookie */);
                MembershipUser myObject = Membership.GetUser();
                Guid UserID = (Guid)myObject.ProviderUserKey;

                MyProfile profile = new MyProfile();
                profile.Address = model.Address;
                profile.City = model.City;
                profile.Zip = model.Zip;
                profile.State = model.State;
                profile.UserId = UserID;

                Debug.Write(profile.State);

                db.aspnet_Profiles.Add(profile);
                return RedirectToAction("Index", "Home");
            }
            else
            {
                ModelState.AddModelError("", AccountValidation.ErrorCodeToString(createStatus));
            }
        }

This is my MyProfile Class:

namespace MatchGaming.Models
{
    [Bind(Exclude = "ProfileId")]
    public class MyProfile
    {
        [Key]
        [ScaffoldColumn(false)]
        public int ProfileId { get; set; }

        public Guid UserId { get; set; }

        [DisplayName("Address")]
        public string Address { get; set; }

        [DisplayName("City")]
        public string City { get; set; }

        [DisplayName("Zip")]
    public string Zip { get; set; }

    [DisplayName("State")]


     public string State { get; set; }
}
}

After the linq query is executed, i check my database and nothing is added. I am using POCO for my entities. Here is my class:

namespace MatchGaming.Models
{
    public class MGEntities : DbContext
    {
        public DbSet<MyProfile> aspnet_Profiles { get; set; }
    }
}

I basically just dont understand why its not adding to the database, if theres a way I can check if the query went through correctly or not or if anyone can see the problem. Thank you!


Try

db.aspnet_Profiles.Add(profile);
db.SaveChanges();


You're misusing EF.

EF contexts are not thread safe and cannot be reused across requests.
You need to create a separate context (MGEntities) for each request, by creating it in the controller in a using statement.

You also need to call SaveChanges().

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜