Problem Adding Objects to context using Entity Framework 4.1 RRS feed

  • Question

  • I am using Code First with EF4.1. I have a model that looks a little like this (It's way more complex than this, but I'm hoping this is simple enough to ask my question):

    public class Applicant
    public int Id { get; set; }
    public string Name { get; set; }
    public string Address { get; set; }

    I am attempting to create an applicant and a coapplicant and save them to the database.


    However, only the coApplicant gets saved to the database. I am assuming it is because the Id is zero on them both, so the coApplicant is either overwriting or merging with the primaryApplicant on the Add. But, I have no idea how to get around it and get it to save them both. The id is autogenerated on insert by the database, so I can't assign the Id before I add them to the context.



    Saturday, October 1, 2011 8:47 PM

All replies

  • This must work. I made a console demo. Just copy and paste it in a new console project and try it.. Just edit an app.config and change the connection string..


    //Add an app.config
    //<?xml version="1.0" encoding="utf-8" ?>
    //  <connectionStrings>
    //    <add name="MyContext"   
    // providerName="System.Data.SqlClient"
    // connectionString="data source=localhost;
    // initial catalog=MyContext;persist security info=True;Trusted_Connection=True;
    // MultipleActiveResultSets=True;Integrated Security=SSPI" />
    // </connectionStrings> //</configuration> using System; using System.Data.Entity; namespace ConsoleApplication1 { public class Applicant { public int Id { get; set; } public string Name { get; set; } public string Address { get; set; } } public class MyContext : DbContext { public DbSet<Applicant> Applicants { get; set; } } internal class Program { private static void Main(string[] args) { using (var context = new MyContext()) { context.Applicants.Add(new Applicant {Address = "Teststr. 1", Name = "Test1"}); context.Applicants.Add(new Applicant {Address = "Teststr. 2", Name = "Test2"}); context.SaveChanges(); } using (var context = new MyContext()) { foreach (var app in context.Applicants) Console.WriteLine(app.Name); } } } }




    Sunday, October 2, 2011 10:17 AM
  • Hi, 

    I guess this should work as you expected.

    When you add new entity,  EF attaches the entity to the context with the EntityStatus as Added. and call on SaveChanges thie record will be inserted into DB.

    I can think of one scenario that results into 1 record in DB is, primaryApplicant and coApplicant are equal by hashcode. Only this scenario context checks on existance with same "Added" status and does not perform one more entity with Added status.

    Are you sure primaryApplicant and coApplicant are not equal?

    Hope this helps you....

    If this post answers your question, please click "Mark As Answer". If this post is helpful please click "Mark as Helpful".
    • Edited by Kris444 Monday, October 3, 2011 6:35 AM Format
    Monday, October 3, 2011 3:56 AM
  • Hi,

    The class property values are pretty close, but there are some differences. The sample I posted is WAY simpler than my actual classes. It may be that the hashcode is the same... how would one check that?

    I wound up getting around it by calling SaveChanges then reinstantiating a new DBContext. It's a hack, I know, but until I find a way around it, I'm stuck doing this.

    Would I be better off posting all my code?


    Tuesday, October 4, 2011 3:05 AM
  • The sample I posted is way simpler than my actual example. I was hoping it would be sufficient to show my problem... I'll try to find a way to post my actual code without giving away private data.

    Thanks for the example though.

    Tuesday, October 4, 2011 3:11 AM
  • Any update? Would you mind letting us know how's going now? If it wan't resolved, I suggest you can directly send the simple demo application to us to analyze your question betteer. My mailbox: v-xugong@microsoft.com


    Larcolais Gong[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, October 4, 2011 4:48 AM