c# - EF Code first one way navigation property -


i have following entities:

[knowntype(typeof(script))] public class application : ientity {     public long id { get; set; }     public string name { get; set; }     public long? startscriptid { get; set; }     // other properties...      // navigation properties:     public virtual script startscript { get; set; }   // new     public virtual list<script> scripts { get; set; } }  [knowntype(typeof(application))] public class script : ientity {     public long id { get; set; }     public string name { get; set; }     // other properties...      // navigation properties:     public virtual application application { get; set; } } 

i have no fluent configurations on dbcontext

so model exists of scripts must bound application. applications can have startscript defined. have working.

now in need of navigation property: application.startscript.

the question how can add navigation property on application without having add equivalent navigation property on script.

thanks in advance!



edit: when run add-migration following migration code generated:

public override void up() {     dropforeignkey("dbo.scripts", "applicationid", "dbo.applications");     addcolumn("dbo.scripts", "application_id", c => c.long());     createindex("dbo.applications", "startscriptid");     createindex("dbo.scripts", "application_id");     addforeignkey("dbo.applications", "startscriptid", "dbo.scripts", "id");     addforeignkey("dbo.scripts", "application_id", "dbo.applications", "id"); } 

this creates new column on scripts don't need, have startscriptid column.


edit: update after @haim770 answer

protected override void onmodelcreating(dbmodelbuilder modelbuilder) {     modelbuilder.entity<application>().hasoptional(x => x.startscript).withoptionaldependent(x => x.application); }  public override void up() {     dropforeignkey("dbo.scripts", "applicationid", "dbo.applications");     dropindex("dbo.scripts", new[] { "applicationid" });     renamecolumn(table: "dbo.applications", name: "applicationid", newname: "startscript_id");     addcolumn("dbo.scripts", "application_id", c => c.long());     createindex("dbo.applications", "startscript_id");     createindex("dbo.scripts", "application_id");     addforeignkey("dbo.scripts", "application_id", "dbo.applications", "id");     addforeignkey("dbo.applications", "startscript_id", "dbo.scripts", "id"); } 

it doesn't understand should use existing startscriptid. there way point in right direction?


edit: wanted database structure:

applications:

  • id, pk, bigint, not null
  • name, nvarchar(max), not null
  • startscriptid, bigint, null

scripts:

  • id, pk, bigint, not null
  • name, nvarchar(max), not null
  • applicationid, fk, bigint, not null


edit:

public class application {     [inverseproperty("startscript")]     public long? startscriptid { get; set; }     [foreignkey("startscriptid")]     public virtual script startscript { get; set; } } 

i thinking there no changes needed in database, have tried adding migration -ignorechanges. got entitycommandexecutionexception when querying entities: "invalid column name 'application_id'". entity framework needs configuration telling use startscriptid property.

you can modify mapping using foreignkeyattribute , inversepropertyattribute. if doesn't work way want, can edit migration before applying it.

if want customize column name, try columnattribute:

public class application {     [column("databasecolumnname")]     public long? startscriptid { get; set; }     [foreignkey("startscriptid")]     public virtual script startscript { get; set; } } 

a longer explanation can found here: http://msdn.microsoft.com/de-de/data/gg193958.aspx


Comments

Popular posts from this blog

ruby on rails - RuntimeError: Circular dependency detected while autoloading constant - ActiveAdmin.register Role -

c++ - OpenMP unpredictable overhead -

javascript - Wordpress slider, not displayed 100% width -