gpt4 book ai didi

c# - Entity Framework 关键问题

转载 作者:行者123 更新时间:2023-11-30 19:35:52 26 4
gpt4 key购买 nike

当我尝试登录时出现以下错误:

Project.Model.Identity.UserLogin: : EntityType 'UserLogin' has no key defined. Define the key for this EntityType. UserLogins: EntityType: EntitySet 'UserLogins' is based on type 'UserLogin' that has no keys defined.

我正在尝试在我的 dbcontext 中使用复合键

  // Primary Key
this.HasKey(t => new { t.LoginProvider, t.ProviderKey, t.UserId });

但它似乎无法拾取它,看起来它正在模型中而不是上下文中搜索键,因为当我在模型中添加“[Key]”关键字时它提示必须将 HasKey 用于复合键

public partial class UserLogin
{
[Key]
public string LoginProvider { get; set; }
[Key]
public string ProviderKey { get; set; }
[Key]
public string UserId { get; set; }
public virtual User User { get; set; }
}

我该如何克服这个问题?我是否要在模型中添加 HasKey?如果是,如何添加以及为什么它没有在上下文中提取它?

最佳答案

如文档中所述,

When you have composite keys, Entity Framework requires you to define an order of the key properties. You can do this using the Column annotation to specify an order.

尝试修改代码如下:

public partial class UserLogin
{
[Key]
[Column(Order=1)]
public string LoginProvider {get; set;}
[Key]
[Column(Order=2)]
public string ProviderKey {get; set; }
// and so on...
}

关于c# - Entity Framework 关键问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50813224/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com