Solved

Foreign keys and navigation properties

Posted on 2011-09-03
4
636 Views
Last Modified: 2012-05-12
I was curious in how foreign keys and navigation properties work in this example. I'm building class library for an e-Commerce solution with the Entity Framework. For example, I have an abstract entity named 'Address' with two derived classes 'BillingAddress' and 'ShippingAddress'. Each customer can have many billing and shipping addresses on file.

Looking at the code files I attached, I'm expecting to use the scalar properties as foreign keys. However, look in File ID 495742. Are what I'm thinking are foreign keys are not acting as foreign keys? For example, could I just get rid of 'StateID' since EF already generates a foreign key 'State_StateID' for me? I would hate to think that I would have to insert the same values in two different columns.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace eCommerceCL.Entities
{
    public abstract class Address
    {
        public int AddressID { get; set; }
        public string StreetAddress { get; set; }
        public string PoBox { get; set; }
        public string City { get; set; }
        public string Zip { get; set; }

        #region
        // An address is associated with a state.
        public int StateID { get; set; }
        public State State { get; set; }
        #endregion

        #region
        // An address is associated with a customer.
        public int UserID { get; set; }
        public Customer Customer { get; set; }
        #endregion
    }
}

Open in new window

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace eCommerceCL.Entities
{
    public class BillingAddress : Address
    {
        public int BillingAddressID { get; set; }
    }
}

Open in new window

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace eCommerceCL.Entities
{
    public class ShippingAddress : Address
    {
        public int ShippingAddressID { get; set; }
        public string BusinessName { get; set; }
    }
}

Open in new window

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace eCommerceCL.Entities
{
    public class Customer
    {
        public int UserID { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public DateTime DateAdded { get; set; }

        #region
        // A customer is a customer type.
        public int CustomerTypeID { get; set; }
        public CustomerType CustomerType { get; set; }
        #endregion

        #region
        // A customer may have many addresses.
        public ICollection<Address> Addresses { get; set; }
        #endregion

        #region
        // A customer may have many credit cards.
        public ICollection<CreditCard> CreditCards { get; set; }
        #endregion

        #region
        // A customer may have many email addresses.
        public ICollection<EmailAddress> EmailAddresses { get; set; }
        #endregion

        #region
        // A customer may have many phone numbers.
        public ICollection<PhoneNumber> PhoneNumbers { get; set; }
        #endregion
    }
}

Open in new window

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace eCommerceCL.Entities
{
    public class State
    {
        public int StateID { get; set; }
        public string Name { get; set; }

        #region
        // A state is associated with an address.
        public Address Address { get; set; }
        #endregion
    }
}

Open in new window

EFModel.jpg
SQLPrintScreen.jpg
0
Comment
Question by:Keith1985
  • 2
  • 2
4 Comments
 
LVL 96

Expert Comment

by:Bob Learned
ID: 36480485
I don't believe that you should be removing any fields that participate in a relationship.   If I understand the Entity Framework correctly, the navigation properties still point to the underlying entity properties.  Navigation properties can be generated to help you with the two-way relationship between tables.  You can choose to have them generated or not when you create the model.  

Navigation Properties
http://msdn.microsoft.com/en-us/library/bb738520.aspx

In a foreign key association, you can form or change a relationship by setting the foreign key property of the dependent object,
0
 

Author Comment

by:Keith1985
ID: 36481026
@TheLearnedOne

So what you are basically stating is that I still need to provide a value for 'StateID' which is a scalar value, but I also need to provide a value for 'State_StateID' which is generated from the association? I just wanted to get clarity on this because I think it is a bit odd.
0
 
LVL 96

Accepted Solution

by:
Bob Learned earned 500 total points
ID: 36484491
No, you don't have to provide a value for the association.  When you want to set a State for the Address, set the Address.State = stateObject.  The Entity Framework should take care of the rest.
0
 

Author Closing Comment

by:Keith1985
ID: 36507446
Thank you very much for the clarity.
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
I have a large data set and a SSIS package. How can I load this file in multi threading?
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question