none
An erorr occured not exposing foreign keys... RRS feed

  • Question

  • An error occurred while saving entities that do not expose foreign key properties for their relationships. The EntityEntries property will return null because a single entity cannot be identified as the source of the exception. Handling of exceptions while saving can be made easier by exposing foreign key properties in your entity types. See the InnerException for details.

    Hello everyone,

    Above is my error. I have a User, Address and a Contact class.

    The User class will have 1 to 1 relationship with address and also with contact. So every User has his own address and contact data.

    The User class will also have 1 to many relationship with Address.

    The Address will have many to many relationship with Contact.

    When I started working on the one to one relationship then all the problems came. It is error after error. At the moment I am out of ideas. So here is how I defined the one to one relationship:

        public class User
        {
            [Key]
            public Guid Id { get; set; }
            public string Username { get; set; }
            public string Password { get; set; }
            public string Email { get; set; }
            public DateTime RegistrationDate { get; set; }
            public DateTime? LastLoginDate { get; set; }
            public int Role { get; set; }
            public static User Current { get; set; }
    
            // one User will have one Contact    
            public virtual Contact UserContact  { get; set; }
           
    
            // one User will have one Address
            public virtual Address UserAddress { get; set; }
           
            // one User will have multiple Addresses
            public virtual ICollection<Address> Address { get; set; }
        }

    I could define AddressID and ContactID and fill them in by hand, but I would like to consider my coding as working with objects. More like User object contains Address object and address object contains a Contact object. So for now I am wondering why i get this error and what to do about it. And the other hand I am curious what is the best way to go.

    Greetings,

    Spacelama

    Wednesday, January 30, 2013 7:18 PM

Answers

  • Hi Spacelama,

    Welcome to the MSDN forum.

    I test this simple sample on my side. It works. Please check it:

        public class User
        {
            public Guid Id { get; set; }
            public String Name { get; set; }
    
            public virtual Contact UserContact { get; set; }
            public virtual Address UserAddress { get; set; }
    
            public virtual ICollection<Address> Addresses { get; set; }
        }
    
        public class Contact
        {
            public Guid Id { get; set; }
            public String Name { get; set; }
            public virtual User User { get; set; }
    
            public virtual ICollection<Address> Addresses { get; set; }
        }
    
        public class Address
        {
            public Guid Id { get; set; }
            public String Name { get; set; }
            public virtual User User { get; set; }
            public virtual ICollection<Contact> Contacts { get; set; }
    
            [ForeignKey("UserMany")]
            public Guid UserId { get; set; }
            public virtual User UserMany { get; set; }
        }
    
        public class tstContext : DbContext
        {
            public DbSet<User> Users { get; set; }
            public DbSet<Contact> Contacts { get; set; }
            public DbSet<Address> Addresses { get; set; }
    
            protected override void OnModelCreating(DbModelBuilder modelBuilder)
            {
                //base.OnModelCreating(modelBuilder);
                modelBuilder.Entity<User>().HasOptional(p => p.UserContact).WithRequired();
                modelBuilder.Entity<User>().HasOptional(p => p.UserAddress).WithRequired();
                modelBuilder.Entity<Address>().HasRequired(p => p.UserMany).WithMany();
            }
        }
    
        class Program
        {
            static void Main(string[] args)
            {
                using (var context = new tstContext())
                {
                    User user = new User
                    {
                        Name = "Alexander",
                        UserAddress = new Address
                        {
                            Name = "ABC"
                        },
                        UserContact = new Contact
                        {
                            Name="Contact"
                        }
                    };
                    context.Users.Add(user);
                    Console.WriteLine();
                    context.SaveChanges();
                }
            }
        }
    

    Good day.


    Alexander Sun [MSFT]
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Alexander Sun Thursday, February 7, 2013 6:55 AM
    Friday, February 1, 2013 3:30 AM

All replies

  • Hi Spacelama,

    Welcome to the MSDN forum.

    I test this simple sample on my side. It works. Please check it:

        public class User
        {
            public Guid Id { get; set; }
            public String Name { get; set; }
    
            public virtual Contact UserContact { get; set; }
            public virtual Address UserAddress { get; set; }
    
            public virtual ICollection<Address> Addresses { get; set; }
        }
    
        public class Contact
        {
            public Guid Id { get; set; }
            public String Name { get; set; }
            public virtual User User { get; set; }
    
            public virtual ICollection<Address> Addresses { get; set; }
        }
    
        public class Address
        {
            public Guid Id { get; set; }
            public String Name { get; set; }
            public virtual User User { get; set; }
            public virtual ICollection<Contact> Contacts { get; set; }
    
            [ForeignKey("UserMany")]
            public Guid UserId { get; set; }
            public virtual User UserMany { get; set; }
        }
    
        public class tstContext : DbContext
        {
            public DbSet<User> Users { get; set; }
            public DbSet<Contact> Contacts { get; set; }
            public DbSet<Address> Addresses { get; set; }
    
            protected override void OnModelCreating(DbModelBuilder modelBuilder)
            {
                //base.OnModelCreating(modelBuilder);
                modelBuilder.Entity<User>().HasOptional(p => p.UserContact).WithRequired();
                modelBuilder.Entity<User>().HasOptional(p => p.UserAddress).WithRequired();
                modelBuilder.Entity<Address>().HasRequired(p => p.UserMany).WithMany();
            }
        }
    
        class Program
        {
            static void Main(string[] args)
            {
                using (var context = new tstContext())
                {
                    User user = new User
                    {
                        Name = "Alexander",
                        UserAddress = new Address
                        {
                            Name = "ABC"
                        },
                        UserContact = new Contact
                        {
                            Name="Contact"
                        }
                    };
                    context.Users.Add(user);
                    Console.WriteLine();
                    context.SaveChanges();
                }
            }
        }
    

    Good day.


    Alexander Sun [MSFT]
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Alexander Sun Thursday, February 7, 2013 6:55 AM
    Friday, February 1, 2013 3:30 AM
  • I was getting the vague foreign key error as well in EF. Until I realized I was trying to set a 800 length string on a column with attributes for MaxLength(200) and Required. Not until I increased the MaxLength to 1000, did I get a descriptive error message.

    Joseph Nielsen

    Wednesday, October 23, 2013 8:26 PM