Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/36.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# 如何在代码优先的MVC应用程序中为应用程序用户(User?,AspNetUser?)创建外键?_C#_Asp.net_Asp.net Mvc_Asp.net Identity_Data Annotations - Fatal编程技术网

C# 如何在代码优先的MVC应用程序中为应用程序用户(User?,AspNetUser?)创建外键?

C# 如何在代码优先的MVC应用程序中为应用程序用户(User?,AspNetUser?)创建外键?,c#,asp.net,asp.net-mvc,asp.net-identity,data-annotations,C#,Asp.net,Asp.net Mvc,Asp.net Identity,Data Annotations,我觉得自己像个白痴,我在谷歌上搜索了所有地方,但我找不到答案如何为应用程序用户Id创建外键?我有一门课: public class Enrollment { public int Id { get; set; } [Required] public Guid UserId { get; set; } [ForeignKey("ApplicationUserId")] public ApplicationUser User { get; set; }

我觉得自己像个白痴,我在谷歌上搜索了所有地方,但我找不到答案如何为应用程序用户Id创建外键?我有一门课:

public class Enrollment
{
    public int Id { get; set; }

    [Required]
    public Guid UserId { get; set; }
    [ForeignKey("ApplicationUserId")]
    public ApplicationUser User { get; set; }

    [Required]
    [ForeignKey("Major")]
    public int MajorId { get; set; }
    [ForeignKey("MajorId")]
    public Major Major { get; set; }

    [Required]
    [ForeignKey("Term")]
    public int TermId { get; set; }
    [ForeignKey("TermId")]
    public Term Term { get; set; }
}
我的错误是:

Unable to determine the principal end of an association between the types 'Plasty.Models.Enrollment' and 'Plasty.Models.ApplicationUser'. The principal end of this association must be explicitly configured using either the relationship fluent API or data annotations.
我试过太阳底下的一切。起初,我想让ApplicationUser拥有另一个属性,注册Id的外键,但这不起作用,所以现在我只是将用户Id放入注册表中

以下是我添加到应用程序用户的两个属性:

 public virtual Enrollment EnrollmentId { get; set; }
 public virtual List<CourseTaken> CoursesTaken { get; set; }
公共虚拟注册注册ID{get;set;}
公共虚拟列表CoursesTaken{get;set;}

你很接近了。用户参数缺少虚拟声明

public class Enrollment {
    //... other get/set methods
    public virtual ApplicationUser User {get; set;}
}

只有当您有多个相同类型的用户时,才需要指定ForeignKey名称。

请尝试以下操作,看看是否有帮助。原因在课堂上的评论中。根据Pengivan的回答,“虚拟”在需要的地方也添加了

public class Enrollment
{
    public int Id { get; set; }

    [Required]
    // The default column is a string, although it stores a Guid
    public string UserId { get; set; } 
    // If specified, should refer to the field name on this side of the relationship
    // As Pengivan stated, not needed unless there are multiple references.
    [ForeignKey("UserId")] 
    public virtual ApplicationUser User { get; set; }

    [Required]
    public int MajorId { get; set; }
    public virtual Major Major { get; set; }

    [Required]
    public int TermId { get; set; }
    public virtual Term Term { get; set; }
}
你有:

public Guid UserId { get; set; }
[ForeignKey("ApplicationUserId")]
public ApplicationUser User { get; set; }

您正在将
applicationserid
指定为外键,但是
Enrollment
上的外键属性的名称是
UserId
。确保名称匹配。:-)

请注意,该标签用于有关图案的问题;在询问有关所述模式的特定于ASP.NET的实现的问题时使用(和/或特定于版本的标记)。在这种情况下,我倾向于认为ASP.NET不理解模型的映射。您的注册模型引用ApplicationUser,ApplicationUser引用注册(CoursesTaken也是如此)。你确定这对你的问题是必要的吗?您正在调用一个多对多数据模式,在该模式中,使用受到限制,调试可能是一件令人头痛的事情。考虑删除Apple用户中的注册和CurrStAcKEN参数。可以通过简单的查询找到它们。