K
K
kohan_kohan2014-09-02 00:48:42
Entity Framework
kohan_kohan, 2014-09-02 00:48:42

How to update entity in entity framework with one to one relationship?

Hello. Faced the following problem while working with entity framework. There are models:

public class Test
    {
        [Key]
        public int Id { get; set; }

        [Required]
        public string Title { get; set; }

        [Required]
        public int CategoryId { get; set; }

        public virtual Category Category { get; set; }

        [Required]
        public virtual TestsDescription TestsDescription { get; set; }
        ..........
    }

 public class TestsDescription
    {
        [Key]
        public int Id { get; set; }

        ..........
    }

But if the object needs to be updated, and I do it like this:
void IRepositoryForEntity<T>.Update(T entity)
        {
           ............
            _db.Entry(entity).State = EntityState.Modified;
        }

Then the data that is directly in the table with tests is updated, but further, the associated TestDescription object is not updated.
Please tell me how can I solve this problem?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Denis Antonenko, 2014-09-02
@kohan_kohan

According to MSDN for TestDescription, you also need to change the status to Modified manually.
Alternatively, check out this package: GraphDiff

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question