C# EntityType';MyProfile';没有定义键。定义此EntityType的键
我不确定为什么会收到此错误消息。我在sql数据库中为它定义了一个主键。这是我的密码:C# EntityType';MyProfile';没有定义键。定义此EntityType的键,c#,asp.net,asp.net-mvc-3,C#,Asp.net,Asp.net Mvc 3,我不确定为什么会收到此错误消息。我在sql数据库中为它定义了一个主键。这是我的密码: [HttpPost] public ActionResult Register(RegisterModel model) { if (ModelState.IsValid) { // Attempt to register the user MembershipCreateStatus createStatus =
[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”未定义键。定义此EntityType的键。
当它尝试添加到数据库时db.Profiles.add(profile)代码> 哪个字段是您的密钥?无论是-ProfileId还是UserId,都可以将名称更改为MyProfileId或Id,或者在其上添加一个[Key]属性。我也遇到了这个问题,并想补充一点,在Entity Framework版本6中,发生此错误的原因是DBGGography类型已从assembly system.data.Entity移动到entityframework.dll中
要在EF 6+中解决此问题,请删除对实体dll的引用,并将using语句更改为
使用System.Data.Entity.Spatial
看到这个了吗