Entity Framework - Mapping Foreign Key to Business Key instead of Primary KeyEntity Framework vs LINQ to SQLMapping a Parent/Child Relationship of a Composite Primary Key TableAutomatically set foreign key in grandchild table in Entity Framework 6EF multiple foreign key relationship on same primary keyEntity Framework - DB-First - Composite Foreign KeysCode First Entity Framework 6: 1 to 1 with composite keyEntity Framework code-first set foreign key with primary key in same tableHow do I specify the foreign key in a one-to-one/zero relationship?Entity Framework relationship without foreign key constrantEntity Framework relationship with two foreign keys

How is the claim "I am in New York only if I am in America" the same as "If I am in New York, then I am in America?

What is the word for reserving something for yourself before others do?

Have astronauts in space suits ever taken selfies? If so, how?

Minkowski space

LaTeX closing $ signs makes cursor jump

Why dont electromagnetic waves interact with each other?

Why does Kotter return in Welcome Back Kotter?

If I cast Expeditious Retreat, can I Dash as a bonus action on the same turn?

Font hinting is lost in Chrome-like browsers (for some languages )

How did the USSR manage to innovate in an environment characterized by government censorship and high bureaucracy?

Writing rule stating superpower from different root cause is bad writing

Do VLANs within a subnet need to have their own subnet for router on a stick?

Smoothness of finite-dimensional functional calculus

Theorems that impeded progress

How do I create uniquely male characters?

Why are electrically insulating heatsinks so rare? Is it just cost?

Risk of getting Chronic Wasting Disease (CWD) in the United States?

Why do falling prices hurt debtors?

To string or not to string

can i play a electric guitar through a bass amp?

Can I ask the recruiters in my resume to put the reason why I am rejected?

How to format long polynomial?

How does strength of boric acid solution increase in presence of salicylic acid?

Modeling an IPv4 Address



Entity Framework - Mapping Foreign Key to Business Key instead of Primary Key


Entity Framework vs LINQ to SQLMapping a Parent/Child Relationship of a Composite Primary Key TableAutomatically set foreign key in grandchild table in Entity Framework 6EF multiple foreign key relationship on same primary keyEntity Framework - DB-First - Composite Foreign KeysCode First Entity Framework 6: 1 to 1 with composite keyEntity Framework code-first set foreign key with primary key in same tableHow do I specify the foreign key in a one-to-one/zero relationship?Entity Framework relationship without foreign key constrantEntity Framework relationship with two foreign keys






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








0















I have two tables with a parent/child relationship, for example:



public class Business

public int Id get; set; //pk
public int ABN get; set; //Business Key
public virtual ICollection<Contract> Contracts get; set;


public class Contract

public int Id get; set; //PK
public virtual Business Business get; set;
public int ABN get; set; //FK



I'd like to map the relationship from child to parent on the business key, NOT the primary key. I thought the following in FluentAPI may do the trick, but I can't work out how to map to the BK instead of PK.



 modelBuilder.Entity<Contract>()
.HasRequired(l => l.Business)
.WithMany(f => f.Contracts)
.HasForeignKey(l => l.ABN)


Am I missing something?










share|improve this question




























    0















    I have two tables with a parent/child relationship, for example:



    public class Business

    public int Id get; set; //pk
    public int ABN get; set; //Business Key
    public virtual ICollection<Contract> Contracts get; set;


    public class Contract

    public int Id get; set; //PK
    public virtual Business Business get; set;
    public int ABN get; set; //FK



    I'd like to map the relationship from child to parent on the business key, NOT the primary key. I thought the following in FluentAPI may do the trick, but I can't work out how to map to the BK instead of PK.



     modelBuilder.Entity<Contract>()
    .HasRequired(l => l.Business)
    .WithMany(f => f.Contracts)
    .HasForeignKey(l => l.ABN)


    Am I missing something?










    share|improve this question
























      0












      0








      0








      I have two tables with a parent/child relationship, for example:



      public class Business

      public int Id get; set; //pk
      public int ABN get; set; //Business Key
      public virtual ICollection<Contract> Contracts get; set;


      public class Contract

      public int Id get; set; //PK
      public virtual Business Business get; set;
      public int ABN get; set; //FK



      I'd like to map the relationship from child to parent on the business key, NOT the primary key. I thought the following in FluentAPI may do the trick, but I can't work out how to map to the BK instead of PK.



       modelBuilder.Entity<Contract>()
      .HasRequired(l => l.Business)
      .WithMany(f => f.Contracts)
      .HasForeignKey(l => l.ABN)


      Am I missing something?










      share|improve this question














      I have two tables with a parent/child relationship, for example:



      public class Business

      public int Id get; set; //pk
      public int ABN get; set; //Business Key
      public virtual ICollection<Contract> Contracts get; set;


      public class Contract

      public int Id get; set; //PK
      public virtual Business Business get; set;
      public int ABN get; set; //FK



      I'd like to map the relationship from child to parent on the business key, NOT the primary key. I thought the following in FluentAPI may do the trick, but I can't work out how to map to the BK instead of PK.



       modelBuilder.Entity<Contract>()
      .HasRequired(l => l.Business)
      .WithMany(f => f.Contracts)
      .HasForeignKey(l => l.ABN)


      Am I missing something?







      .net entity-framework-6 ef-fluent-api






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Mar 22 at 0:02









      DylanBDylanB

      404215




      404215






















          1 Answer
          1






          active

          oldest

          votes


















          0














          Upon further investigation, the way to do this is with the HasPrincipalKey() feature. For example:



           modelBuilder.Entity<Contract>()
          .HasRequired(l => l.Business)
          .WithMany(f => f.Contracts)
          .HasForeignKey(l => l.ABN)
          .HasPrincipalKey(b => b.ABN)


          Disappointingly however, this is only available in EntityFramework Core not EntityFramework 6.2.






          share|improve this answer























          • Yeah, support for alternate keys was introduced in EF Core. EF 6 can only use PK references.

            – Ivan Stoev
            Mar 22 at 2:19











          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55290958%2fentity-framework-mapping-foreign-key-to-business-key-instead-of-primary-key%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          Upon further investigation, the way to do this is with the HasPrincipalKey() feature. For example:



           modelBuilder.Entity<Contract>()
          .HasRequired(l => l.Business)
          .WithMany(f => f.Contracts)
          .HasForeignKey(l => l.ABN)
          .HasPrincipalKey(b => b.ABN)


          Disappointingly however, this is only available in EntityFramework Core not EntityFramework 6.2.






          share|improve this answer























          • Yeah, support for alternate keys was introduced in EF Core. EF 6 can only use PK references.

            – Ivan Stoev
            Mar 22 at 2:19















          0














          Upon further investigation, the way to do this is with the HasPrincipalKey() feature. For example:



           modelBuilder.Entity<Contract>()
          .HasRequired(l => l.Business)
          .WithMany(f => f.Contracts)
          .HasForeignKey(l => l.ABN)
          .HasPrincipalKey(b => b.ABN)


          Disappointingly however, this is only available in EntityFramework Core not EntityFramework 6.2.






          share|improve this answer























          • Yeah, support for alternate keys was introduced in EF Core. EF 6 can only use PK references.

            – Ivan Stoev
            Mar 22 at 2:19













          0












          0








          0







          Upon further investigation, the way to do this is with the HasPrincipalKey() feature. For example:



           modelBuilder.Entity<Contract>()
          .HasRequired(l => l.Business)
          .WithMany(f => f.Contracts)
          .HasForeignKey(l => l.ABN)
          .HasPrincipalKey(b => b.ABN)


          Disappointingly however, this is only available in EntityFramework Core not EntityFramework 6.2.






          share|improve this answer













          Upon further investigation, the way to do this is with the HasPrincipalKey() feature. For example:



           modelBuilder.Entity<Contract>()
          .HasRequired(l => l.Business)
          .WithMany(f => f.Contracts)
          .HasForeignKey(l => l.ABN)
          .HasPrincipalKey(b => b.ABN)


          Disappointingly however, this is only available in EntityFramework Core not EntityFramework 6.2.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Mar 22 at 0:17









          DylanBDylanB

          404215




          404215












          • Yeah, support for alternate keys was introduced in EF Core. EF 6 can only use PK references.

            – Ivan Stoev
            Mar 22 at 2:19

















          • Yeah, support for alternate keys was introduced in EF Core. EF 6 can only use PK references.

            – Ivan Stoev
            Mar 22 at 2:19
















          Yeah, support for alternate keys was introduced in EF Core. EF 6 can only use PK references.

          – Ivan Stoev
          Mar 22 at 2:19





          Yeah, support for alternate keys was introduced in EF Core. EF 6 can only use PK references.

          – Ivan Stoev
          Mar 22 at 2:19



















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55290958%2fentity-framework-mapping-foreign-key-to-business-key-instead-of-primary-key%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

          Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

          Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript