Deleting a child in many ways using POCO in EF4 CTP5

Short version:

Using the classic example of Order and OrderLine, how can I do the following in POCO:

public partial class Order {

    public void RemoveInvalidOrderLines() {
        OrderLines.Remove(OrderLines.Where(ol => ol.Quantity > MaxQuantity));
    }
}

In a many-to-one relationship using POCO, this only removes the reference to Order in the OrderLine. Is there a way to delete an OrderLine object automatically? Or do I need to insert a repository or raise code in a service? Use trigger in database? Is there a sample or best practice for this? All the examples that I find on the Internet seem to use a few simple additions and "That's why you should use POCOs" :-)

Situation:

Using EF4 CTP5 in VS2010, generating POCOs that are not saved (dynamic proxies), I struggle with how to handle the removal of related child objects.

: Remove ICollection Relative Employee , . , / ? , POCO.

: , add, delete .. Employee. , .: -)

: . Employee, . , Employee , FK-. ? POCOs.

, :

Employee:             
Id int (PK, not null)
Name varchar

Relative:
 Id int (PK, not null)
 EmployeeId int (FK, not null)
 Name varchar

POCOs:

public partial class Employee {
...
    public virtual ICollection<Relative> Relatives
    {

        get
        {
            if (_relatives == null)
            {
                var newCollection = new FixupCollection<Relative>();
                newCollection.CollectionChanged += FixupRelatives;
                _relatives = newCollection;
            }
            return _relatives;
        }
        set
        {
            if (!ReferenceEquals(_relatives, value))
            {
                var previousValue = _relatives as FixupCollection<Relative>;
                if (previousValue != null)
                {
                    previousValue.CollectionChanged -= FixupRelatives;
                }
                _relatives = value;
                var newValue = value as FixupCollection<Relative>;
                if (newValue != null)
                {
                    newValue.CollectionChanged += FixupRelatives;
                }
            }
        }
    }

, ,

Relative:
public partial class Relative {
...
 public virtual Employee Employee
    {
        get { return _employee; }
        set
        {
            if (!ReferenceEquals(_employee, value))
            {
                var previousValue = _employee;
                _employee = value;
                FixupEmployee(previousValue);
            }
        }
    }
    private Employee _employee;


}

:

var employeeRepository = new EmployeeRepository(dbContext);
var employee = employeeRepository.Get(1234);
var relative = new Relative { Name = "Lars" };
employee.Relatives.Add(relative); 
context.SaveChanges();

Relative .

, DeleteRelative Employee:

public void DeleteRelative(string name) {
     Relatives.Remove(Relatives.FirstOrDefault());
}

( , , ..)

:

System.InvalidOperationException: The operation failed: The relationship could not be changed because one or more of the foreign-key properties is non-nullable. When a change is made to a relationship, the related foreign-key property is set to a null value. If the foreign-key does not support null values, a new relationship must be defined, the foreign-key property must be assigned another non-null value, or the unrelated object must be deleted.

, EmployeeId Relative .

, -, , / ( POCO).

, script, , , EF4 , / .

, , .

, .: -)

+3
1

, . , . , , ( , SaveChanges() ).

,

  • .
  • ( , " " ).
  • .

, , , .

- ( Entity Framework):

this.ContextOptions.ProxyCreationEnabled = true;
+1

Source: https://habr.com/ru/post/1782856/


All Articles