Solved

Foreign keys and navigation properties

Posted on 2011-09-03
4
634 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

863 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now