的EntityType“我的资料”没有定义键。定义此的EntityType关键 [英] EntityType 'MyProfile' has no key defined. Define the key for this EntityType

查看:104
本文介绍了的EntityType“我的资料”没有定义键。定义此的EntityType关键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道为什么我收到此错误信息。我在为它的SQL数据库中定义的主键。这里是我的code:

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 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;
                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; }


    }
}

我不知道为什么我收到此错误:的EntityType我的资料没有定义键。定义此的EntityType关键当它试图添加到数据库 db.Profiles.Add(配置文件);

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);.

推荐答案

哪些领域是你的钥匙?无论它是 - 配置文件ID或用户ID - 要么将名称更改为MyProfileId或ID或者把[关键]属性就可以了。

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.

这篇关于的EntityType“我的资料”没有定义键。定义此的EntityType关键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆