简体   繁体   English

EntityType'MyProfile'没有定义键。 定义此EntityType的键

[英]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. 我在我的sql数据库中为它定义了一个主键。 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: 这是我的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; }


    }
}

I am not sure why I am getting this error: EntityType 'MyProfile' has no key defined. Define the key for this EntityType. 我不知道为什么我收到此错误: EntityType 'MyProfile' has no key defined. Define the key for this EntityType. EntityType 'MyProfile' has no key defined. Define the key for this EntityType. when it tries to add to the database db.Profiles.Add(profile); 当它试图添加到数据库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. 无论它是什么 - ProfileId或UserId - 将名称更改为MyProfileId或Id,或者在其上放置[Key]属性。

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 我也遇到了这个问题,并希望在Entity Framework版本6中添加此错误,因为DbgGeography类型已从程序集system.data.entity移动到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 要在EF 6+中解决此问题,请删除对实体dll的引用,并将using语句更改为使用System.Data.Entity.Spatial

see this http://entityframework.codeplex.com/workitem/1535 请参阅此http://entityframework.codeplex.com/workitem/1535

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 EntityType“帖子”没有定义键。 定义此EntityType的键 - EntityType 'posts' has no key defined. Define the key for this EntityType EntityType 'Worker' 没有定义键。 定义此 EntityType 的键 - EntityType 'Worker' has no key defined. Define the key for this EntityType EntityType没有定义键。 定义此EntityType的键 - EntityType has no key defined. Define the key for this EntityType EntityType X没有定义键。 定义此EntityType的键 - EntityType X has no key defined. Define the key for this EntityType EF 5.0 Fluent映射:“ EntityType'{ClassMap}'”未定义键。 定义此EntityType的密钥。” - EF 5.0 Fluent mapping: “EntityType '{ClassMap}' has no key defined. Define the key for this EntityType.” 模型验证错误:EntityType 未定义键。 定义此 EntityType 的键 - Model validation error: EntityType has no key defined. Define the key for this EntityType 数据库第一个EntityType'表'没有定义键。 定义此EntityType的键 - Database First EntityType 'table' has no key defined. Define the key for this EntityType 代码优先-EntityType'编译器结果'尚未定义键。 定义此EntityType的键 - Code First - EntityType 'Compiler Results' has no key defined. Define the key for this EntityType EntityType''没有定义键。 定义此EntityType的键。 -C#Web API实体框架 - EntityType ' ' has no key defined. Define the key for this EntityType. - C# Web API Entity Framework 实体框架4.3.1 - > 5.0异常:“EntityType没有定义键。 定义此EntityType的键。“ - Entity Framework 4.3.1 -> 5.0 Exception: “EntityType has no key defined. Define the key for this EntityType.”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM