Click here to Skip to main content
15,895,192 members

c#, EF, Code First, Repository Pattern and One-to-One relationships

Brett Hargreaves asked:

Open original thread
Hi,

Could somebody help me please, I'm not understanding the repository pattern very well!

I have a model whereby I have a Person entity with a one-to-one link with a PersonProfile Entity.

So I have my two entities:
public class person
{
    public int PersonId {get;set;}
    public string Name {get;set;}
    ....
    public int PersonProfileId {get;set;}
    public virtual PersonProfile PersonProfile {get;set;}
}

public class PersonProfile
{
    public int PersonProfileId {get;set;}
    ....
}


I have a repository for Person and a service for creating the person.
Now I need to make sure that whenever a Person is create a Personprofile is also created and linked (with blank data in it). The idea being prevent me having to check further down the line that the records exist before I try to update or display them.

But how would be best to do it? Create a personprofile repository and send through a reference when I call the create person service? Do it from my controller page and make sure it's all hooked up before I pass the object to the person create service?

i.e.

personprofile profile = new person profile();
personprofileservice.create(profile);
personprofileservice.save();

person person = new person();
person.personprofile = profile;
personservice.create(person);
personservcice.save();


Or is there a better way I'm not just not understandng?

Thanks in advance.
Tags: C#, Entity Framework, Patterns, Repository

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900