Entity Framework doesn't generate ApplicationU

2019-06-06 14:11发布

问题:

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!

回答1:

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.