首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >EntityType 'MyProfile‘未定义任何键。定义此EntityType的密钥

EntityType 'MyProfile‘未定义任何键。定义此EntityType的密钥
EN

Stack Overflow用户
提问于 2011-03-15 10:40:27
回答 2查看 24K关注 0票数 18

我不确定为什么会收到此错误消息。我在sql数据库中为它定义了一个主键。下面是我的代码:

代码语言:javascript
复制
[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类:

代码语言:javascript
复制
   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; }


    }
}

我不确定为什么在尝试添加到数据库db.Profiles.Add(profile);时会出现这个错误:EntityType 'MyProfile' has no key defined. Define the key for this EntityType.

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-03-15 10:45:45

哪个字段是您的关键字?无论是ProfileId还是UserId,要么将名称更改为MyProfileId或Id,要么为其添加一个键属性。

票数 36
EN

Stack Overflow用户

发布于 2014-01-20 09:45:07

我也遇到了这个问题,我想补充一点,在实体框架版本6中发生这个错误是因为DbgGeography类型已经从程序集system.data.entity移到了entityframework.dll中

要在EF dll中解决此问题,请移除对实体6+的引用,并将using语句更改为using System.Data.Entity.Spatial

查看此http://entityframework.codeplex.com/workitem/1535

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5306947

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档