locked
Insert record using Entityframework 3.5 RRS feed

  • Question

  • I have got two tables in database 

    1) State (stateid,statename)

    2) City(cityid, stateid, cityname)

    I am using Entity framework for db manipulation

    I want to insert data into City table with the reference to State Table using entity framework 

    For that i wrote the following code

    MyModel mdl= new MyModel();
    City ct=new City();
    
    ct.cityname="Dallas";
    ct.State=mdl.State.Where(u=>u.stateid==1);
    
    mdl.AddToCity(ct);
    mdl.SaveChanges();

    There are data in "state" table.

    But i am getting following error at "mdl.AddToCity(ct)"

    "An entity object cannot be referenced by multiple instances of IEntityChangeTracker"


    markand

    Sunday, July 22, 2012 5:42 PM

Answers

  • Hi Markand;

    Seeming that the documentation states that Single and First method where in Entity Framework 1 in .Net Framework 3.5 it will help to see the schema of the two tables as can be gotten from SQL Server Management tool, the platform you are implementing this on and the actual code that is being used.

      


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    • Marked as answer by Alexander Sun Friday, August 3, 2012 2:14 AM
    Monday, July 23, 2012 2:10 PM

All replies

  • Hi MarkandBhatt;

    A Linq query will return a collection of objects and a collection can't be assign to an object of a single type such as ct.State therefore you need to return a single type from this query, mdl.State.Where(u=>u.stateid==1), to do that change this statement :

    ct.State=mdl.State.Where(u=>u.stateid==1);

    to this statement :

    ct.State=mdl.State.Where(u=>u.stateid==1).Single();

    If the table State does not have a stateid which you query for then you will want to use this version :

    ct.State=mdl.State.Where(u=>u.stateid==1).SingleOrDefault();

    which will assign a null to ct.State if stateid is not found.

      


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    • Proposed as answer by Alan_chen Monday, July 23, 2012 2:52 AM
    Sunday, July 22, 2012 7:05 PM
  • The methods Single() and SingleOrDefault() are not working.

    I am getting following error

    The method 'Single' is not supported by LINQ to Entities. Consider using the method 'First' instead.

    And if i use First() i get "An entity object cannot be referenced by multiple instances of IEntityChangeTracker"


    markand

    Monday, July 23, 2012 3:19 AM
  • Hi Markand,

    Sounds like the object was attached to another context and not detached. Please ensure whether the entity object that this statement (mdl.State.Where(u=>u.stateid==1).First()) returned is referenced by another context or not.

    Good day.


    Alexander Sun [MSFT]
    MSDN Community Support | Feedback to us

    Monday, July 23, 2012 6:12 AM
  • Hi Markand;

    Seeming that the documentation states that Single and First method where in Entity Framework 1 in .Net Framework 3.5 it will help to see the schema of the two tables as can be gotten from SQL Server Management tool, the platform you are implementing this on and the actual code that is being used.

      


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    • Marked as answer by Alexander Sun Friday, August 3, 2012 2:14 AM
    Monday, July 23, 2012 2:10 PM