Entity Framework - Add Navigation Property Manually
Yup - it's not that straightforward.
Here's what you do:
1 - Right click on the designer, Add -> Association
2 - Setup the association and cardinalities (People *..1 Gender, People *..1 Race)
3 - Go into the Model Browser -> Associations
4 - Right click on your newly created associations, click Properties
5 - Here you need to setup the endpoints for the key and cascade options. Make sure you get the endpoints correct. You can also setup a referential constraint here for your implicit navigational property.
6 - Map the navigational property to the relevant tables/fields.
7 - Validate your model, cross your fingers.
Add Navigation Property Manually to Entity Framework
It is not possible because code is not PK in any of your tables. Navigation properties follows same rules as database relations - in principal table you must use PK and in dependent you specify FK. Databases also offers selecting unique key in principal table but EF doesn't support unique keys yet.
How to include a navigation property without selecting a new Model?
The problem was, I have a project with 2 Framework Versions (Entity Framework 6 and Entity Framework Core).
I was using the Include
method from:
using System.Data.Entity
Instead of the correct one:
using Microsoft.EntityFrameworkCore
How can you add a navigation property on a view in EF 6.1 CodeFirst
In EF you can use a database views and map it to an entity and reference it just as you do with tables.
For code first process you have to create the View in Up and drop it in Down methods from migration class:
public partial class AddView : DbMigration
{
public override void Up()
{
this.Sql(@"CREATE VIEW MyView1 AS ....");
}
public override void Down()
{
this.Sql(@"DROP VIEW MyView1....");
}
}
EDIT:
public long myTable2Id { get; set; }
[ForeignKey( "myTable2Id" )]
public virtual MyTable2 Table2 {get;set;}
Related Topics
Groupby on Complex Object (E.G. List<T>)
How to Set Dynamic Value in My Attribute
Detect If Geolocation Is in Complex Polygon or Not
How to Get the Access Token from a Blazor (Server-Side) Web App
Itextsharp: Adjust 2 Elements on Exactly One Page
What Are C# Lambda's Compiled Into? a Stackframe, an Instance of an Anonymous Type, Or
Data Contract Serializer - How to Omit the Outer Element of a Collection
Stop SQL Query Execution from .Net Code
C#: HTMLagilitypack Extract Inner Text
Install to Same Path When Upgrading Application
Ftpwebrequest Ftp Download with Progressbar
How to Deserialize Xml with Dynamic Element Names
Get Values from Process Standardoutput
Manual Editing of *.Designer.Cs File
How to Create a Button That Can Send Keys to a Conrol Without Losing Focus - Virtual Keyboard