2

I am having an issue inserting a record for a dependent entity when inserting the parent. Below is my entity definitions and mappings

EBUser

public partial class EBUser : ModelBase, IUser<long>
{
    public string UserName { get; set; }
    public string Password { get; set; }
    public long AccountId { get; set; }
    public EBAccount EbAccount { get; set; }
    public string Email { get; set; }
    public DateTime CreatedDate { get; set; }
    public DateTime? LastUpdateDate { get; set; }

    public virtual EBUserInfo UserInfo { get; set; }
}

EBUserInfo

public partial class EBUserInfo : ModelBase
{
    public string Email { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string DisplayName { get; set; }
    public virtual EBUser User { get; set; }
}

EBUserMapping

public class EBUserMapping : BaseEntityMapping<EBUser>
{
    public EBUserMapping()
    {
        this.ToTable("Users");
        this.Property(u => u.AccountId).HasColumnName("AccountId");
        this.Property(u => u.Password).HasColumnName("Password");
        this.Property(u => u.UserName).HasColumnName("UserName");
        this.Property(u => u.Email).HasColumnName("Email");
        this.Property(u => u.CreatedDate).HasColumnName("CreatedDate");
        this.Property(u => u.LastUpdateDate).HasColumnName("LastUpdateDate").IsOptional();

        //this.HasRequired(u => u.UserInfo).WithRequiredDependent(u => u.User);
        this.HasRequired(t => t.EbAccount)
            .WithMany(t => t.Users)
            .HasForeignKey(d => d.AccountId);
    }
}

EBUserInfoMapping

public class EBUserInfoMapping :BaseEntityMapping<EBUserInfo>
{
    public EBUserInfoMapping()
    {
        this.ToTable("UserInfo");
        this.Property(u => u.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
        this.Property(u => u.Email).HasColumnName("Email");
        this.Property(u => u.FirstName).HasColumnName("FirstName");
        this.Property(u => u.LastName).HasColumnName("LastName");
        this.Property(u => u.DisplayName).HasColumnName("DisplayName");

        // Relationships
        this.HasRequired(t => t.User).//WithRequiredDependent(t => t.UserInfo);
        WithOptional(t => t.UserInfo);
    }
}

In the database schema, all tables have an ID column but in the EBUserInfor class the Id column is both the primary and foreign key to the EBUsers table.

The BaseEntityuMapping maps the Id column and set the DatabaseGenerationOptions to identity but in the EBUserinfoMapping class I overwrite that with a DatabaseGenerationOption of None.

When I insert a new EBUser record using Entity Framework, the user record is created but no userInfo record is created

Please help

Thanks

5
  • Why did you abandon the WithRequiredDependent(t => t.UserInfo) part? That's exactly the mapping you need here. Commented Dec 26, 2015 at 16:09
  • That's the first property I set but the UserInfor dependent still did not insert, when the User object was inserted. I am now thinking I need to insert the UserInfo object and let Entity Framework auto insert the principal. I will try that and update this thread if it works Commented Dec 26, 2015 at 16:31
  • Nope did not work using WithRequitedDependent or WithOptional Commented Dec 26, 2015 at 16:38
  • Well, I don't see what you do exactly and what "doesn't work" means. The only mapping between the two should be this.HasRequired(t => t.User).WithRequiredDependent(t => t.UserInfo) (or WithOptional). Commented Dec 26, 2015 at 17:20
  • "doesn't work" means the userInfo record is not created when I save the user object. The userInfo object is hydrated when I retrieve the user object from the context and user object is hydrated when i retrieve the userInfo object. When I try to insert the user object, the userinfo object is not persisted and when I try to the insert the userInfo object, it fails because ef does not attempt to create the user object first to get the Id which will be used for the userInfo object primary key, so inserting the userinfo object fails to insert for a foreign key violation Commented Dec 26, 2015 at 17:43

1 Answer 1

0

Map user to userInfo as

EBuser user= new EBuser();
// fill here user
EBuserInfo info = new EBuserInfo();
Info.userInfo= user;
// fill here rest info
db.add(info);
db.saveChanges();
Sign up to request clarification or add additional context in comments.

8 Comments

So add the dependent entity instead of the principal to the context for the both records to be created in the database?
Unfortunately that did not work. Looking at the generated SQL, only the insert statement for the dependent entity was generated (UserInfo), cause a foreign key constraint violation because the id was 0 and cannot be an identity column
From view when your save userRecored action called then you have to map those values of model which is coming from the view to the entities which are genrated by entity framework. At controller you have to map the user data into the userinfo.user property which is of user type. So first map all values in the user entity from view and then put that user into userinfo.user entity add finally save it.
Please refer this link, same as your scenerio stackoverflow.com/questions/4442915/… hope it will go you through the problem.
According to the post, I have to create instances of bother dependent and principal entities, assigned the principal to a navigation property of the the dependent and then add both dependent and principal to the context before saving. Is this an accurate assessment?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.