Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Foreign keys and navigation properties

Posted on 2011-09-03
4
Medium Priority
?
641 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

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

Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

688 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