Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You need to focus on mapping the entities themselves properly and then relations will just work. The conventions about keys are something along:</p> <ol> <li>If the entity has a property called Id it will be used as key</li> <li>If there is an EntityId (e.g. UserId) property then that is the key</li> <li>You need to specify what the key property is otherwise</li> </ol> <p>As to what column that key property corresponds to in the database, you can specify that using the Property() and HasColumnName() methods.</p> <p>Once this is done you don't have to worry about mapping keys in the relations mapping(well at least in your case one-to-one mapping).</p> <p>As to relationship mapping you don't have to define the relations both ways from User and from Profile. Once you define the relationship one way the other way is inferred. This keeps your code clean as well.</p> <p>The code below should help you understand the whole thing.</p> <pre><code>class Program { static void Main(string[] args) { // replace your connection string here using (var userContext = new UserContext("UserProfile")) { var u = new User { UserId = Guid.NewGuid(), Name = "John", Profile = new Profile() { ProfileId = Guid.NewGuid(), SomeValue = "x" } }; userContext.Users.Add(u); userContext.SaveChanges(); var users = userContext.Users.ToList(); foreach (var user in users) { Console.WriteLine(user.Name); if (user.Profile != null) { Console.WriteLine(user.Profile.ProfileId); } } var profiles = userContext.Profiles.Where(p =&gt; p.User.Name == "John").ToList(); } } } public class UserContext : DbContext { public UserContext(string connectionString) : base(connectionString) { } public DbSet&lt;User&gt; Users { get; set; } public DbSet&lt;Profile&gt; Profiles { get; set; } protected override void OnModelCreating(DbModelBuilder modelBuilder) { base.OnModelCreating(modelBuilder); modelBuilder.Entity&lt;User&gt;().Property(u =&gt; u.UserId).HasColumnName("ID"); modelBuilder.Entity&lt;Profile&gt;().Property(p =&gt; p.ProfileId).HasColumnName("ID"); modelBuilder.Entity&lt;User&gt;().HasOptional(u =&gt; u.Profile).WithRequired(p =&gt; p.User); } } public class User { public Guid UserId { get; set; } public string Name { get; set; } public Profile Profile { get; set; } } public class Profile { public Guid ProfileId { get; set; } public User User { get; set; } public string SomeValue { get; set; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload