0

My question is this. Let's say I have a Category class and Product class. And they are implemented like this : Category :

public class Category { public Category() { this.Products = new ObservableCollection<Product>(); } public int CategoryId { get; set; } public string Name { get; set; } public virtual ObservableCollection<Product> Products { get; private set; } } 

And Product :

public class Product { public int ProductId { get; set; } public string Name { get; set; } public int CategoryId { get; set; } public virtual Category Category { get; set; } } 

My question is this. If their id names were both "Id", how could I set the same relationship between Category and Product? In this example I can easily put CategoryId in product because the IDs have different names. What if they had the same name? What should I do? Thanks.

7
  • You haven't, but how can I achieve the same relationship? Is there anything extra to do? Both Category and Product is inherited from the same base class. And the class has "Id". Commented Jul 7, 2014 at 7:54
  • Have you had a look at the msdn.microsoft.com/en-gb/data/jj591620.aspx article. Search for 'Configuring a Many-to-Many Relationship'. You need to use the modelBuilder to map the relationship. Commented Jul 7, 2014 at 7:54
  • You don't need them to have the same name. If you want the name in the database tables to be identical, you can do that by using the [Column(Name="ID")] attribute to decorate your ProductId and CategoryId fields in their respective class definitions. Commented Jul 7, 2014 at 7:57
  • @AlexBarac They have to be ID in class definitions also. Commented Jul 7, 2014 at 8:00
  • @jason Is there a particular reason for that constraint? Commented Jul 7, 2014 at 8:04

1 Answer 1

1

I think just renaming their Id(s) to "Id" work perfectly as you expected.

public class Category { public Category() { this.Products = new ObservableCollection<Product>(); } public int Id { get; set; } public string Name { get; set; } public virtual ObservableCollection<Product> Products { get; private set; } } public class Product { public int Id { get; set; } public string Name { get; set; } public int CategoryId { get; set; } public virtual Category Category { get; set; } } 

Result

Result from database

Sign up to request clarification or add additional context in comments.

7 Comments

So you mean there is nothing additional thing to do to set the relationship?
you need to understand the convention, but if you want different name as navigation property you can use ForeignKey attribute or FluentApi
the convention fits your requirement, so yes, there is nothing to set up
How does it know thaat? Categories_CategoryId is same as the Id of categories? Thanks.
in this case by adding Category::Products(array) creates the relationship, if you don't have Product::CategoryId, then EF will create Category_Id column in Products table
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.