Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/338.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/heroku/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 无法检索元数据。在模型生成过程中检测到一个或多个验证错误。Entitytype没有定义键_C#_Entity Framework_Ef Code First - Fatal编程技术网

C# 无法检索元数据。在模型生成过程中检测到一个或多个验证错误。Entitytype没有定义键

C# 无法检索元数据。在模型生成过程中检测到一个或多个验证错误。Entitytype没有定义键,c#,entity-framework,ef-code-first,C#,Entity Framework,Ef Code First,我在我的项目中使用EF代码优先的方法。我有一个“订单”模型,其外键“UserID”为“ApplicationUser”模型,如下所示: public class Order { [Key] public int OrderID { get; set; } public virtual string UserID { get; set; } public decimal TotalPrice { get; set; } public bool Order

我在我的项目中使用EF代码优先的方法。我有一个“订单”模型,其外键“UserID”为“ApplicationUser”模型,如下所示:

public class Order
{
    [Key]
    public int OrderID { get; set; }

    public virtual string UserID { get; set; }
    public decimal TotalPrice { get; set; }

    public bool OrderShipped { get; set; }
    public DateTime OrderDateTime { get; set; }
    public string Address1 { get; set; }
    public string Address2 { get; set; }
    public string Town { get; set; }
    public string Country{ get; set; }
    public string PostalCode { get; set; }

    [ForeignKey("UserID")]
    public virtual ApplicationUser User { get; set; }
}

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
    public ApplicationDbContext()
        : base("DefaultConnection", throwIfV1Schema: false)
    {
    }

    public static ApplicationDbContext Create()
    {
        return new ApplicationDbContext();
    }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);
    }
}
公共类秩序
{
[关键]
公共int-OrderID{get;set;}
公共虚拟字符串UserID{get;set;}
公共十进制总价{get;set;}
公共bool{get;set;}
public DateTime OrderDateTime{get;set;}
公共字符串地址1{get;set;}
公共字符串地址2{get;set;}
公共字符串{get;set;}
公共字符串国家{get;set;}
公共字符串PostalCode{get;set;}
[外键(“用户ID”)]
公共虚拟应用程序用户{get;set;}
}
公共类ApplicationDbContext:IdentityDbContext
{
公共应用程序上下文()
:base(“DefaultConnection”,throwifvv1schema:false)
{
}
公共静态应用程序上下文创建()
{
返回新的ApplicationDbContext();
}
模型创建时受保护的覆盖无效(DbModelBuilder modelBuilder)
{
基于模型创建(modelBuilder);
}
}
问题是,每当我尝试添加模型类为'Order.cs(MajorProject2.Models)'和数据上下文类为'MajorProject2Context(MajorProject2.Models)'的新控制器时,它都会给出如下错误:

运行所选代码生成器时出错:“无法检索“MajorProject2.Models.Order”的metada”

在模型生成过程中检测到一个或多个验证错误:

IdentityUserLogin::EntityType“IdentityUserLogin”未定义键。 定义此EntityType的

IdentityUserRole::EntityType“IdentityUserRole”未定义键。 定义此EntityType的

IdentityUserLogins:EntityType:EntitySet“IdentityUserLogins”基于未定义键的类型“IdentityUserLogin”

IdentityUserRoles:EntityType:EntitySet“IdentityUserRoles”基于未定义键的类型“IdentityUserRole”


如何解决这个错误??我尝试过相关问题的不同解决方案,但没有一个有效。

我有两个DbContext:MajorProject2Context和ApplicationDbContext。由于在MajorProjectContext中的一个模型中使用了ApplicationDbContext中的外键,所以出现了该错误。我找到的唯一解决方案是将两个DbContext合并为一个DbContext(ApplicationDbContext)。我不得不做了一些修改,但现在一切都正常了。

你只是忘了给你的课程顺序添加一个ID。你看,EF用你的文件生成表格。它需要名为ID的字段。
因此,将
OrderID
更改为
ID
,一切都必须正常。

您是否有上下文中订单的数据库集?公共虚拟DbSet Orders{get;set;}我在上下文中确实有用于Orders的DbSet。但它仍然不起作用。什么是MajorProject2Context?这与ApplicationDbContext相同吗?该错误通常与未正确生成标识模型有关。MajorProject2Context是另一个Dbcontext。我通过创建单个DbContext而不是使用两个DbContext解决了这个错误。这是在应用程序上下文中重用身份模型的一种方法。另一种方法是在第二个上下文中复制applicationuser类,并添加一个DbSet。