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.

jason
  • 6,962
  • 36
  • 117
  • 198
  • 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". – jason Jul 07 '14 at 07:54
  • Have you had a look at the http://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. – Dr Schizo Jul 07 '14 at 07: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. – Alex Barac Jul 07 '14 at 07:57
  • @AlexBarac They have to be ID in class definitions also. – jason Jul 07 '14 at 08:00
  • @jason Is there a particular reason for that constraint? – Alex Barac Jul 07 '14 at 08:04
  • @AlexBarac it's a requirement that I can't change. – jason Jul 07 '14 at 08:07
  • I would prefer to refer http://stackoverflow.com/questions/5542864/how-should-i-declare-foreign-key-relationships-using-code-first-entity-framework. I hope this would give you an idea. – Jayaraj.K Jul 07 '14 at 08:46

1 Answers1

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

Yuliam Chandra
  • 14,494
  • 12
  • 52
  • 67
  • So you mean there is nothing additional thing to do to set the relationship? – jason Jul 07 '14 at 08:14
  • you need to understand the [convention](http://msdn.microsoft.com/en-us/data/jj679962.aspx), but if you want different name as navigation property you can use [ForeignKey attribute](http://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.schema.foreignkeyattribute%28v=vs.110%29.aspx) or [FluentApi](http://msdn.microsoft.com/en-us/data/jj591617.aspx) – Yuliam Chandra Jul 07 '14 at 08:17
  • the convention fits your requirement, so yes, there is nothing to set up – Yuliam Chandra Jul 07 '14 at 08:20
  • How does it know thaat? Categories_CategoryId is same as the Id of categories? Thanks. – jason Jul 07 '14 at 08:30
  • It maps the name supplied in the code with the name of the column in the database. If the two names are not identical, then the name of the column is set as an attribute, similar to the one I mentioned in my comment above, for the name of the field in the code. – Alex Barac Jul 07 '14 at 08:35
  • the convention bro, "In the Entity Framework, navigation properties provide a way to navigate a relationship between two entity types. Every object can have a navigation property for every relationship in which it participates. Navigation properties allow you to navigate and manage relationships in both directions, returning either a reference object (if the multiplicity is either one or zero-or-one) or a collection (if the multiplicity is many). Code First infers relationships based on the navigation.." - [EF Relationship Convention](http://msdn.microsoft.com/en-us/data/jj679962.aspx) – Yuliam Chandra Jul 07 '14 at 08:41
  • 1
    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 – Yuliam Chandra Jul 07 '14 at 08:44