Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EF foreign key reference using Id vs object

What is the difference between foreign key reference using Id vs object.

For example: FK relation using Id

class Product
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string PhoneNumber { get; set; }

    public int CategoryId { get; set; }
}

vs FK relation using object

class Product
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string PhoneNumber { get; set; }

    public Category Category { get; set; }
}

I have noticed from database that by using Id property the column essentially becomes non-null field.

Is that the only difference? I think we can't query multiple tables at once i.e.querying related data?

When should I choose to use either options?

like image 719
wenn32 Avatar asked Feb 20 '19 13:02

wenn32


People also ask

What is OnModelCreating in Entity Framework?

The DbContext class has a method called OnModelCreating that takes an instance of ModelBuilder as a parameter. This method is called by the framework when your context is first created to build the model and its mappings in memory.

Do we use foreign keys in Entity Framework?

No foreign key propertyWhile it is recommended to have a foreign key property defined in the dependent entity class, it is not required.

How do you add a foreign key to EF?

The [ForeignKey(name)] attribute can be applied in three ways: [ForeignKey(NavigationPropertyName)] on the foreign key scalar property in the dependent entity. [ForeignKey(ForeignKeyPropertyName)] on the related reference navigation property in the dependent entity.

How do I get identity value in EF core?

EF execute each INSERT command followed by SELECT scope_identity() statement. SCOPE_IDENTITY returns the last identity value inserted into an identity column in the same scope. The above example will execute the following SQL in the database. WHERE @@ROWCOUNT = 1 AND [StudentID] = scope_identity();


1 Answers

In your first example you're not adding a relationship, just an integer property named CategoryId. In your second example, Entity Framework will create an integer column named "Category_ID", but you will be not be able to see this property in your model, so I like to explicitly add it my self and be able to use it along with the navigation property.

class Product
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string PhoneNumber { get; set; }


    [ForeignKey("Category")]
    public int CategoryId { get; set; }
    public Category Category{get;set;}
}

This way you can also control the data type of CategoryId, so you could make it optional (nullable)

public int? CategoryId { get; set; }

*The foreign key data annotation is not needed, unless you have property or navigation property names that do not follow the naming convention for foreign key property names (Bardr), it doesn't harm to explicitly declare it either for clarity purposes

This implies that you're creating a 1 to many relationship (1-*) with products and categories, so in your Category class you would be adding a collection navigation property for products

class Category
{
  public int Id{ get; set;}
  public string Name{ get; set; }
  ...
  public ICollection<Product> Products{get; set;}
}
like image 65
The One Avatar answered Oct 12 '22 14:10

The One