实体框架不会生成ApplicationUser外键 [英] Entity Framework doesn't generate ApplicationUser foreign key

查看:197
本文介绍了实体框架不会生成ApplicationUser外键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

注意:对于这个问题,我已经简化了模型。



我正在使用ASP.NET Core MVC应用程序(使用Entity Framework Core)。我有三个模型。我有一个ApplicationUser(从IdentityUser扩展),我有一个活动模型,我有一个'SignUp'模型有一个活动的外键和ApplicationUser的外键。至少,这是这个想法。事情是,实体框架将FK作为活动FK,而不是应用程序用户。这只是在没有任何FK约束的数据库中的另一个int。



我尝试了很多我在互联网上找到的东西,但我无法得到它上班。这是我目前所拥有的(缩略为清晰):

活动:

 <$ c ()

SignUps = new HashSet< SignUp>(); $ c $ public $ {

$ b public Activity
}

public int ActivityID {get;组; }
$ b $必须的
[StringLength(50)]
[Display(Name =Naam)]
public string Name {get;组; }

public virtual ICollection< SignUp> SignUps {get;组;




ApplicationUser:





$ b <公共类ApplicationUser:IdentityUser
{
public ApplicationUser()
{

Activities = new HashSet< Activity>( );

$ b $必需的
显示(名称=Voornaam)]
public string FirstName {get;组; }
$ b $必须的
显示(Name =Achternaam)]
public string LastName {get;组; }

public virtual ICollection< Activity>活动{get;组;



$ b

注册:

  public class SignUp 
{
public int SignUpID {get;组; }

[必需的]
public int ActivityID {get;组; }

[ForeignKey(ApplicationUser)]
public int ApplicationUserID {get;组; }

[必须]
公共字符串状态{get;组; }注意:我第一次没有[ForeignKey]属性(如with活动),但它是在那里,因为我试图让它工作。

正如我上面所说,SignUp中的活动FK工作正常,Applicationuser FK没有。实体框架只是在数据库中生成一个没有任何FK约束的int,当脚手架CRUD页面时,它也不被识别为FK。



我对ASP很新颖。净MVC和EF和我有我觉得很简单的东西的感觉。 Facepalm瞬间传入?

感谢!

解决方案 Activity 关系可以工作,因为您有导航属性。 Activity 具有 ICollection< SignUp> ,所以EF绑定到 ActivityID SignUp 上维护该集合。与 ApplicationUser 没有任何相似之处。您只需要一个名为 ApplicationUserID 的属性; EF不会做任何事情,因为它不知道该怎么做。



只要添加一个导航属性,就会很好: / p>

  [ForeignKey(ApplicationUser)] 
public int ApplicationUserID {get;组; }
public ApplicationUser ApplicationUser {get;组; }

编辑

实际上,在看了你的代码之后,我想你可能已经把 ICollection< Activity> 弄乱了 ApplicationUser ApplicationUser Activity 之间没有直接关系。这种关系通过 SignUp 来实现,它基本上是一个有效载荷的M2M。因此,您应该有以下内容:

  public class ApplicationUser:IdentityUser 
{
public ApplicationUser()
{

Activities = new HashSet< Activity>();

$ b $必需的
显示(名称=Voornaam)]
public string FirstName {get;组; }
$ b $必须的
显示(Name =Achternaam)]
public string LastName {get;组; }

public virtual ICollection< SignUp> SignUps {get;组; }



$ b $ p
$ b然后,它的功能和你的活动
关系目前。


Note: for this question, I've simplified the models a bit.

Im working on an ASP.NET Core MVC application (with Entity Framework Core). I have three models. I have an ApplicationUser (that extends from IdentityUser), I have an Activity model and I have a 'SignUp' model that has a foreign key to an activity and a foreign key to ApplicationUser. At least, that's the idea. The thing is, Entity Framework recognices the Activity FK as a FK, but not the applicationuser. That just becomes another int in the database without any FK constraints.

I've tried a lot of things that I found on the internet, but I can't get it to work. This is what I currently have (shortened for clarity):

Activity:

public class Activity
{
    public Activity()
    {
        SignUps = new HashSet<SignUp>();
    }

    public int ActivityID { get; set; }

    [Required]
    [StringLength(50)]
    [Display(Name = "Naam")]
    public string Name { get; set; }

    public virtual ICollection<SignUp> SignUps { get; set; }
}

ApplicationUser:

public class ApplicationUser : IdentityUser
{
    public ApplicationUser()
    {

        Activities = new HashSet<Activity>();
    }

    [Required]
    [Display(Name = "Voornaam")]
    public string FirstName { get; set; }

    [Required]
    [Display(Name = "Achternaam")]
    public string LastName { get; set; }

    public virtual ICollection<Activity> Activities { get; set; }
}

SignUp:

public class SignUp
{
    public int SignUpID { get; set; }

    [Required]
    public int ActivityID { get; set; }

    [ForeignKey("ApplicationUser")]
    public int ApplicationUserID { get; set; }

    [Required]
    public string Status { get; set; }
}

Note: I first did not have the [ForeignKey] attribute (like with activity), but it's there because I was trying to get it working.

As I said above, the Activity FK in SignUp works fine, the Applicationuser FK doesn't. Entity Framework just generates an int in the database without any FK constraints and when scaffolding CRUD pages, it's also not recognized as an FK.

I'm quite new to ASP.net MVC and EF and I have the feeling I'm overlooking something very simple. Facepalm moment incoming?

Thanks!

解决方案

The Activity relationship works because you have navigation properties involved there. Activity has an ICollection<SignUp>, so EF is binding to ActivityID on SignUp to maintain that collection. There is nothing similar being done with ApplicationUser. You just have an property called ApplicationUserID; EF will not do anything with that because it has no idea it should.

Just add a navigation property and you'll be good:

[ForeignKey("ApplicationUser")]
public int ApplicationUserID { get; set; }
public ApplicationUser ApplicationUser { get; set; }

EDIT

Actually, after looking at your code a bit more, I think you may have just messed up with the ICollection<Activity> on ApplicationUser. There's no direct relationship between ApplicationUser and Activity. That relationship comes via SignUp, which is basically an M2M with a payload. As a result, you should have the following instead:

public class ApplicationUser : IdentityUser
{
    public ApplicationUser()
    {

        Activities = new HashSet<Activity>();
    }

    [Required]
    [Display(Name = "Voornaam")]
    public string FirstName { get; set; }

    [Required]
    [Display(Name = "Achternaam")]
    public string LastName { get; set; }

    public virtual ICollection<SignUp> SignUps { get; set; }
}

Then, it would function exactly the same as your Activity relationship does currently.

这篇关于实体框架不会生成ApplicationUser外键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆