27663

setup relation for 1 to 1 with entity framework

Question:

How do I have to setup the modelbuilder for the ContactId?

A CountryCompanyAssignment has a relation to a Contact.

When I delete the CountryCompanyAssignment I do not want that the contact is lost.

When I create a CountryCompanyAssignment then I also need a contact to save the CountryCompanyAssignment.

I need to setup the relation between CountryCompanyAssignment and the ContactId because when I delete the CountryCompanyAssignment I get an error saying I should add a foreign key for the Contact to make it work.

This is what I tried:

modelBuilder.Entity<CountryCompanyAssignment>().HasRequired(e => e.Contact).WithRequiredDependent(e => e.); [Key] [Column(Order = 1)] public int Id { get; set; } [Key] [Column(Order = 1)] public string ContactId { get; set; } [Required] public string Test{ get; set; } [Required] public virtual Contact Contact { get; set; }

Answer1:

if you use a WithRequired, one of the PK must be a FK:

<ul><li>Contact.Id if WihtRequiredPrincipal</li> <li>Company.Id if WithRequiredDependent</li> </ul>

if you want to <strong>expose and choose</strong> the fk you must:

modelBuilder.Entity<Company>().HasRequired(x => x.Contact). WithMany().HasForeignKey(y => y.ContactId);

Recommend

  • AudioUnitInitialize failed with error code 1701737535 'ent?' after alarm interruption
  • get address name from WCF service
  • What is the idiomatic way to handle/unwrap nested Result types?
  • Web api expand error 'object' does not contain a definition for 'Include'
  • Insert First Set of Data To EF Code-First Using Repository Pattern.
  • Include external XML ENTITIES using SYSTEM while resolving path with $ENV variable
  • Reference a parent table for a Navigation Property in EF
  • Why is this column getting generated in EF Code-First migrations?
  • ASP.NET MVC Page Validation Fails (The value is invalid.)
  • Map weak entity by using Code first
  • Configuring cascade delete with EF7
  • converting Twilio sms body to a string - encoding error for @ symbol
  • How to resolve “ Multiplicity is not valid in Role” error?
  • Many to Many in Linq using Dapper
  • Extending the Django 1.11 User Model
  • How to Model Ternary Relationship in CakePhp?
  • Are there any side effects from calling SQLAlchemy flush() within code?
  • Why value captured by reference in lambda is broken? [duplicate]
  • R: gsub and capture
  • jqPlot EnhancedLegendRenderer plugin does not toggle series for Pie charts
  • Comma separated Values
  • Hits per day in Google Big Query
  • JTable with a ScrollPane misbehaving
  • Angular 2 constructor injection vs direct access
  • Why joiner is not used after Sequence generator or Update statergy
  • FormattedException instead of throw new Exception(string.Format(…)) in .NET
  • Java static initializers and reflection
  • Android Google Maps API OnLocationChanged only called once
  • Linking SubReports Without LinkChild/LinkMaster
  • Bitwise OR returns boolean when one of operands is nil
  • XCode 8, some methods disappeared ? ex: layoutAttributesClass() -> AnyClass
  • Easiest way to encapsulate a HTML5 webpage into an android app?
  • Busy indicator not showing up in wpf window [duplicate]
  • Recursive/Hierarchical Query Using Postgres
  • costura.fody for a dll that references another dll
  • Observable and ngFor in Angular 2
  • How to Embed XSL into XML
  • UserPrincipal.Current returns apppool on IIS
  • Conditional In-Line CSS for IE and Others?
  • How to load view controller without button in storyboard?