Solved

Foreign keys and navigation properties

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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

757 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

18 Experts available now in Live!

Get 1:1 Help Now