Entity Framework: Shouldn't attaching the following entity throw an exception?
As far as I can tell, `EntityCollection.Attach` and `EntityReference.Attach` can only create relationships that already exists in the database. In other words, if `Address.EmployeeID == Employee.EmployeeID`, then the following code will work:

            Employee employee = context.Employees.First();
            Address address = context.Addresses.First();
            employee.Addresses.Attach(address);

But if `Address.EmployeeID != Employee.EmployeeID`, then the code will throw an exception:

            Employee employee = context.Employees.First();
            Address address = context.Addresses.First();
            employee.Addresses.Attach(address); // System.InvalidOperationException: A 
                                                // referential integrity constraint
                                                // violation occurred


But according to a code example taken from the following [thread][1], `EntityCollection.Attach` and `EntityReference.Attach` can also be used to create a relationship that doesn't exist in a DB:

    var existingPerson = ctx.Persons.SingleOrDefault(p => p.Name = "Joe Bloggs" };
    var myAddress = ctx.Addresses.First(a => a.PersonID != existingPerson.PersonID);
    existingPerson.Addresses.Attach(myAddress);
    // OR:
    myAddress.PersonReference.Attach(existingPerson)
    ctx.SaveChanges();


So am I correct in assuming that `EntityCollection.Attach` and `EntityReference.Attach` can only create relationships that already exists in the database, and as such code example taken from the other thread should throw an exception?

Thank you  


  [1]: http://stackoverflow.com/questions/3920111/entity-framework-4-addobject-vs-attach
Entity Framework: Shouldn't attaching the following entity throw an exception?
===

As far as I can tell, `EntityCollection.Attach` and `EntityReference.Attach` can only create relationships that already exists in the database. In other words, if `Address.EmployeeID == Employee.EmployeeID`, then the following code will work:

            Employee employee = context.Employees.First();
            Address address = context.Addresses.First();
            employee.Addresses.Attach(address);

But if `Address.EmployeeID != Employee.EmployeeID`, then the code will throw an exception:

            Employee employee = context.Employees.First();
            Address address = context.Addresses.First();
            employee.Addresses.Attach(address); // System.InvalidOperationException: A 
                                                // referential integrity constraint
                                                // violation occurred


But according to a code example taken from the following [thread][1], `EntityCollection.Attach` and `EntityReference.Attach` can also be used to create a relationship that doesn't exist in a DB:

    var existingPerson = ctx.Persons.SingleOrDefault(p => p.Name = "Joe Bloggs" };
    var myAddress = ctx.Addresses.First(a => a.PersonID != existingPerson.PersonID);
    existingPerson.Addresses.Attach(myAddress);
    // OR:
    myAddress.PersonReference.Attach(existingPerson)
    ctx.SaveChanges();


So am I correct in assuming that `EntityCollection.Attach` and `EntityReference.Attach` can only create relationships that already exists in the database, and as such code example taken from the other thread should throw an exception?

Thank you  


  [1]: http://stackoverflow.com/questions/3920111/entity-framework-4-addobject-vs-attach