Entity Framework - Add Navigation Property Manually

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



Leave a reply



Submit