This type is made nullable unless the relationship is detected as or configured as required. For example: protected override void OnModelCreating(ModelBuilder modelBuilder)īy convention, a shadow foreign key gets its type from the principal key in the relationship. A shadow foreign key will also be created if the argument to HasForeignKey does not match any. Shadow foreign key properties are often created by convention. Foreign key properties can be private, which is often a good compromise to avoid exposing the foreign key while allowing its value to travel with the entity. It is therefore often pragmatic to keep foreign key properties in the. If entities are going to be serialized, for example to send over a wire, then the foreign key values can be a useful way to keep the relationship information intact when the entities are not in an object/graph form. This application code then manipulates the relationship entirely through navigations. Shadow foreign keys are usually used when there is a desire to hide the relational concept of a foreign key from the domain model used by application code/business logic. EF keeps track of the property value and state internally. A shadow property exists in the EF model but does not exist on the. For example: protected override void OnModelCreating(ModelBuilder modelBuilder)įoreign key properties can be created as shadow properties. ![]() The same thing can be achieved by explicitly configuring the foreign key property itself as required. The end result of this is that the foreign key column in the database is made non-nullable even if the foreign key property is nullable. Or, if the foreign key is discovered by convention, then IsRequired can be used without a call to HasForeignKey: protected override void OnModelCreating(ModelBuilder modelBuilder) For example: protected override void OnModelCreating(ModelBuilder modelBuilder) However, a nullable foreign key property can used for a required relationship using the attribute, or by calling IsRequired in the model building API. The property name is calculated or constructed based on some input to the model building process.Īs described in Optional and required relationships, the nullability of the foreign key property determines whether a relationship is optional or required.The property or properties do not exist on the entity type and should be created as shadow properties.The property or properties are private.HasForeignKey("ContainingBlogId1", "ContainingBlogId2") Or, for a composite foreign key: protected override void OnModelCreating(ModelBuilder modelBuilder) For example, for a single property: protected override void OnModelCreating(ModelBuilder modelBuilder) HasForeignKey can also be passed the name of the foreign key property as a string. Using lambda expressions in model building API ensures that the property use is available for code analysis and refactoring, and also provides the property type to the API for use in further chained methods.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |