开发者

EntityType 'MyProfile' has no key defined. Define the key for this EntityType

I am not sure why I am getting this error message. I have a primary key defined in my sql database for it. Here is my code:

[HttpPost]
    public ActionResult Register(RegisterModel model)
    {
        if (ModelState.IsValid)
        {
            // Attempt to register the user
            MembershipCreateStatus creat开发者_运维百科eStatus = 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;
                db.Profiles.Add(profile);
                return RedirectToAction("Index", "Home");
            }
            else
            {
                ModelState.AddModelError("", AccountValidation.ErrorCodeToString(createStatus));
            }
        }

        // If we got this far, something failed, redisplay form
        ViewBag.PasswordLength = MembershipService.MinPasswordLength;
        return View(model);
    }

And this is my MyProfile Class:

   namespace MatchGaming.Models
{
    [Bind(Exclude = "ProfileId")]
    public class MyProfile
    {
        [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; }


    }
}

I am not sure why I am getting this error: EntityType 'MyProfile' has no key defined. Define the key for this EntityType. when it tries to add to the database db.Profiles.Add(profile);.


Which field is your key? Whichever it is - ProfileId or UserId - either change the name to MyProfileId or Id or else put a [Key] attribute on it.


I ran into this issue also and wanted to add that in Entity Framework version 6 this error occurs because the DbgGeography type has been moved from the assembly system.data.entity and into the entityframework.dll

to resolve this in EF 6+ remove the reference to the entity dll and change the using statement to using System.Data.Entity.Spatial

see this http://entityframework.codeplex.com/workitem/1535

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜