Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EF Code First Fluent API specifying the Foreign Key property

I have a class AgentBalance with an association to Agent, thus:

public class AgentBalance {     ...      public int AgentId { get; set; }      public virtual Agent Agent { get; set; }  } 

AgentId is detected as the FK for the Agent relationship by convention, but I want to make it explicit in the Mapping class, to be safer against future changes. If Agent had a collection of Balances then I know how to do this e.g.:

HasRequired(t => t.Agent).WithMany(a => a.Balances).HasForeignKey(t => t.AgentId); 

However, Agent does not have a collection of Balances - I don't want that association to be reverse navigable. But without the .WithMany in the mapping I don't get the option to specify .HasForeignKey. Is there another way? (N.B. I know I could also do this using attributes, but I want to use the fluent API mapping).

like image 884
Richard Pawson Avatar asked Oct 14 '13 11:10

Richard Pawson


People also ask

How do you set a foreign key in 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.

Has foreign key fluent API?

The Entity Framework Core Fluent API HasForeignKey method is used to specify which property is the foreign key in a relationship. In the following example, the AuthorFK property in the Book entity does not follow Entity Framework Core's convention for foreign key names.

Does Entity Framework support foreign keys?

When you change the relationship of the objects attached to the context by using one of the methods described above, Entity Framework needs to keep foreign keys, references, and collections in sync.

How do you set composite primary key in fluent API?

The only way to configure composite keys is to use the HasKey method. You specify the properties that form the composite key by passing them in as properties of an anonymous type to the HasKey method.


2 Answers

I believe you should be able to do this:

HasRequired(t => t.Agent).WithMany().HasForeignKey(t => t.AgentId) 
like image 142
SOfanatic Avatar answered Sep 19 '22 18:09

SOfanatic


I prefer to use Data Annotations for these tasks, not Fluent API. It is much shorter end easy to understand. EF must detect properties ended with "Id" automatically, but to be on a safe side you can specify them explicitly:

using System.ComponentModel.DataAnnotations.Schema; ... public int AgentId { get; set; }  [ForeignKey("AgentId")] public virtual Agent Agent { get; set; } 

You will need to specify them explicitly if your FK prop are not ended with "Id", for example:

public int AgentCode { get; set; }  [ForeignKey("AgentCode")] // now this is needed if you'd like to have FK created public virtual Agent Agent { get; set; } 

You can find more details here: https://msdn.microsoft.com/en-us/data/jj591583.aspx

like image 34
fifonik Avatar answered Sep 21 '22 18:09

fifonik