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;
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
add a comment |
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
add a comment |
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
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
.net entity-framework-6 ef-fluent-api
asked Mar 22 at 0:02
DylanBDylanB
404215
404215
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
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.
Yeah, support for alternate keys was introduced in EF Core. EF 6 can only use PK references.
– Ivan Stoev
Mar 22 at 2:19
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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.
Yeah, support for alternate keys was introduced in EF Core. EF 6 can only use PK references.
– Ivan Stoev
Mar 22 at 2:19
add a comment |
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.
Yeah, support for alternate keys was introduced in EF Core. EF 6 can only use PK references.
– Ivan Stoev
Mar 22 at 2:19
add a comment |
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.
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.
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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