簡體   English   中英

EntityType'MyProfile'沒有定義鍵。 定義此EntityType的鍵

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

我不知道為什么我收到此錯誤消息。 我在我的sql數據庫中為它定義了一個主鍵。 這是我的代碼:

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

這是我的MyProfile類:

   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 'MyProfile' has no key defined. Define the key for this EntityType. EntityType 'MyProfile' has no key defined. Define the key for this EntityType. 當它試圖添加到數據庫db.Profiles.Add(profile);

哪個領域是你的關鍵? 無論它是什么 - ProfileId或UserId - 將名稱更改為MyProfileId或Id,或者在其上放置[Key]屬性。

我也遇到了這個問題,並希望在Entity Framework版本6中添加此錯誤,因為DbgGeography類型已從程序集system.data.entity移動到entityframework.dll中

要在EF 6+中解決此問題,請刪除對實體dll的引用,並將using語句更改為使用System.Data.Entity.Spatial

請參閱此http://entityframework.codeplex.com/workitem/1535

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM