Model 3 Taxes to Item

I have a legacy application that I am trying to model in Entity Framework.

I am selling a course that could have 1, 2 or 3 taxes.  

My current tables are as follows:

    public class Course
    {
        public int Id { get; set; }
        public string Name { get; set; }
		public decimal Price {get; set;}
		public int Tax1 {get; set;}
		public int Tax2 {get; set;}
		public int Tax3 {get; set;}
		
		public virtual Tax Tax {get; set;} ??????
	}
	
	public class Tax
	{
		public int Id {get; set;}
		public string Name {get; set;}
		public decimal Percent {get; set;}
	}
	
	My Table of Taxes is as follows:
	
	Id	Name			Percent
	1	Federal Tax		5
	2	Provincial		12
	3	Local			5

Open in new window


I am trying to make this work so that if I had say a course with a provincial tax, I might do something like @Html.EditorFor(model => model.Course.Tax1.Percent) and get the proper value.  I think I have to do something with the fluent api - but quite frankly - can't get my head around this. Any help greatly appreciated.

RBS
RBSAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Bob LearnedCommented:
I don't understand your question.  What are you needing help with?
0
RBSAuthor Commented:
I am trying to link the Taxes table to the Courses Table so that let's say I have a value 2 in the Tax1 field of the Courses table, it will link to the Provincial tax in the Taxes table which has a id of 2 so that I can do something like @Html.EditorFor(model => model.Course.Tax1.Percent), I would get a 12 which is the value of the Provincial Tax.  

Specifically, what I am having a problem with is lnking the Tax1 field in Courses to the Tax1  object whose values I would correspond with the record whose id is 2 in the Taxes table table -

public virtual Tax1 Tax1 {get; set;} ??????
0
Bob LearnedCommented:
You should be able to define an association in the Entity Framework designer, using the Add Association Dialog shown here:

Relationships/Associations with the EF Designer
http://msdn.microsoft.com/en-us/data/jj713299.aspx#Associations

Add Association Dialog in Entity Framework Designer
0
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

RBSAuthor Commented:
Unfortunately, I',m using Code First - don't have designer available.

RBS
0
Bob LearnedCommented:
If you are using code-first with Entity Framework, then you should be able to do something like this:

Associations in EF Code First: Part 6 – Many-valued Associations
http://weblogs.asp.net/manavi/archive/2011/05/17/associations-in-ef-4-1-code-first-part-6-many-valued-associations.aspx

class BidConfiguration : EntityTypeConfiguration<Bid>
{
    internal BidConfiguration()
    {
        this.HasRequired(b => b.Item)
            .WithMany(i => i.Bids)
            .HasForeignKey(b => b.ItemId);
    }
}

Open in new window

0
RBSAuthor Commented:
Thanks - that was very helpful - but am unable to get .HasForeignKey to work - here's what I have so far....

 protected override void OnModelCreating(DbModelBuilder modelBuilder)
       {
           modelBuilder.Entity<Course>().ToTable("Courses");
           modelBuilder.Entity<Course>().Property(p => p.Id)
              .HasColumnName("CourseId");
           modelBuilder.Entity<Course>().Property(p => p.Name)
              .HasColumnName("ListName");
           modelBuilder.Entity<Course>().Property(p => p.Description)
             .HasColumnName("Details");
          
		   //How to say  Foreign Key Course->Tax1 maps to Id in Taxes?????
		   //How to say  Foreign Key Course->Tax2 maps to Id in Taxes?????
		   //How to say  Foreign Key Course->Tax3 maps to Id in Taxes?????
		   
           modelBuilder.Entity<Tax1>().ToTable("Taxes");
           modelBuilder.Entity<Tax1>().Property(p => p.Id)
              .HasColumnName("TaxId");
           modelBuilder.Entity<Tax1>().Property(p => p.Name)
              .HasColumnName("ListName");
			  
			  
			modelBuilder.Entity<Tax2>().ToTable("Taxes");
           modelBuilder.Entity<Tax2>().Property(p => p.Id)
              .HasColumnName("TaxId");
           modelBuilder.Entity<Tax2>().Property(p => p.Name)
              .HasColumnName("ListName");
       }

Open in new window


Specifically how to implement/say

               //How to say  Foreign Key Course->Tax1 maps to Id in Taxes?????
               //How to say  Foreign Key Course->Tax2 maps to Id in Taxes?????
               //How to say  Foreign Key Course->Tax3 maps to Id in Taxes?????

RBS
0
Bob LearnedCommented:
Associations in EF Code First: Part 3 – Shared Primary Key Associations
http://weblogs.asp.net/manavi/archive/2011/04/14/associations-in-ef-4-1-code-first-part-3-shared-primary-key-associations.aspx

In the example shown, User has a BillingAddress navigation property.

An association is created between User.BillingAddress and Addresses.AddressId with the following statement:

 modelBuilder.Entity<User>()
         .HasOptional(u => u.BillingAddress)
         .WithRequired(); 

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
RBSAuthor Commented:
Thanks O LearnedOne - sorry for the delayed response.

RBS
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.