Note that there are some explanatory texts on larger screens.

plurals
  1. POSplitting an Entity with navigational property into two tables using EF4.1 Fluent API
    primarykey
    data
    text
    <p>I have a Users table and a UserProfiles table. A user has either zero or only one User profile. (i.e one to one relationship) can someone help me use EF4.1 fluent API to Map the Users Entity to the both Users and UserProfiles tables. Below is the table details. Also Users and AuthProvider have a one to Many relationship. </p> <pre><code>// 1. Users Table has the columns(UserId(PK), UserName, Email, AuthProviderId(FK)) // 2. UserProfiles Table has columns(UserId(PK/FK), FirstName, MiddleName,LastName) // 3. AuthProvider Table has columns (AuthProviderId(PK), AuthName) public class User { public User() { Id = Guid.NewGuid(); } public virtual Guid Id { get; private set; } public virtual string Username { get; set; } public virtual string Email { get; set; } public virtual AuthProvider AuthProvider { get; set; } public virtual string FirstName { get; set; } public virtual string MiddleName { get; set; } public virtual string LastName { get; set; } } public class AuthProvider { public AuthProvider() { Id = Guid.NewGuid(); } public virtual Guid Id { get; private set; } public virtual string Name { get; set; } public virtual ICollection&lt;User&gt; Users { get; set; } } // This is the Mappings public class UserConfiguration : EntityTypeConfiguration&lt;User&gt; { public UserConfiguration() { HasKey(x =&gt; x.Id).Property(x =&gt; x.Id).HasColumnName("UserId").IsRequired(); Property(x =&gt; x.Username).HasColumnName("Username").IsRequired(); Property(x =&gt; x.Email).HasColumnName("Email").IsRequired(); Property(x =&gt; x.FirstName).HasColumnName("FirstName").IsOptional(); Property(x =&gt; x.MiddleName).HasColumnName("MiddleName").IsOptional(); Property(x =&gt; x.LastName).HasColumnName("LastName").IsOptional(); HasRequired(x =&gt; x.AuthProvider).WithMany(x =&gt; x.Users).Map(x =&gt; x.MapKey("AuthProviderId")); Map(mc =&gt; { mc.Properties(x =&gt; new { x.Id, x.Username, x.Email, x.AuthProvider }); mc.ToTable("Users"); }); Map(mc =&gt; { mc.Properties(x =&gt; new { x.Id, x.FirstName, x.MiddleName, x.LastName }); mc.ToTable("UserProfiles"); }); } } public class AuthProviderConfiuration : EntityTypeConfiguration&lt;AuthProvider&gt; { public AuthProviderConfiuration() { ToTable("AuthProviders"); HasKey(x =&gt; x.Id).Property(x =&gt; x.Id).HasColumnName("AuthProviderId").IsRequired(); Property(x =&gt; x.Name).HasColumnName("ProviderName").IsRequired(); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    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. 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