Click here to Skip to main content
15,879,535 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have the following two classes

C#
// You can add profile data for the user by adding more properties to your ApplicationUser class, please visit http://go.microsoft.com/fwlink/?LinkID=317594 to learn more.
    public class AppUser : IdentityUser
    {
        public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<AppUser> manager)
        {
            // Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
            var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
            // Add custom user claims here
            return userIdentity;
        }

        public int? DomainId { get; set; }

        [ForeignKey("DomainId")]
        public virtual Domain Domain { get; set; }
    }

public class Domain
    {
        [Key]
        public int DomainId { get; set; }

        [Required]
        [MaxLength(100)]
        public string Name { get; set; }

        public virtual ICollection<AppUser> AppUsers { get; set; }
    }


and I am trying to get a one to many relationship setup via dataannotations and I cannot get it to work correctly.

It is creating the field "DomainId" as nullable and with a foreign key to the Domain table, but it is also creating a field called "Domain_DomainId" as nullable and with a foreign key to the Domain table.

How do I get the "Domain_DomainId" field to go away?
Posted

1 solution

You have the wrong item tagged as the foreign key.

C#
public class AppUser : IdentityUser
    {
        public async Task<claimsidentity> GenerateUserIdentityAsync(UserManager<appuser> manager)
        {
            // Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
            var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
            // Add custom user claims here
            return userIdentity;
        }
 
        [ForeignKey("Domain")]
        public int? DomainId { get; set; }
        
        public virtual Domain Domain { get; set; }
    }
</appuser></claimsidentity>
 
Share this answer
 
v2
Comments
steve@stephenraycook.com 19-Sep-14 9:28am    
I tried that, and it gives me an error...

The property 'DomainId' cannot be configured as a navigation property. The property must be a valid entity type and the property should have a non-abstract getter and setter. For collection properties the type must implement ICollection<t> where T is a valid entity type.

So i tried...

[ForeignKey("Domain")]
public int? DomainId { get; set; }

and it gave me the same results as I was originally getting.
Nathan Minier 19-Sep-14 9:35am    
Damn, I need more coffee in the morning. Do you have a specific model builder for this class?

Also, why is DomainId nullable?
steve@stephenraycook.com 19-Sep-14 9:37am    
I guess after thinking about it, it doesn't have to be nullable..
Nathan Minier 19-Sep-14 9:42am    
Removing the nullable should free up the class and allow EF to map it properly.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900