Note that there are some explanatory texts on larger screens.

plurals
  1. POEntityFramework 5.0 Cant save object with childs using SaveContext
    primarykey
    data
    text
    <p>I have a base object with common properties:</p> <pre><code>public abstract class BaseItem { [Required] public User ModifiedUser { get; set; } [Required] public User CreatedUser { get; set; } } </code></pre> <p>All other objects inherit form the above but when I try to save using the SaveContext it doesn't work with error: ModifiedUser - The ModifiedUser field is required. CreatedUser - The CreatedUser field is required.</p> <p>Category object:</p> <pre><code>public class Category : BaseItem, INotifyPropertyChanged { public int CategoryId { get; set; } public string Name { get; set; } ... } </code></pre> <p>Method for saving category:</p> <pre><code>public List&lt;Category&gt; SaveCategories(Category[] categories) { try { using (EntityContext context = new EntityContext()) { context.Category.AddOrUpdate(categories); context.SaveChanges(); } ... } catch (DbEntityValidationException e) { ... } } </code></pre> <p>I used AddOrUpdate() but I also tried to get a category from database first and then call the context.SaveChanges(); but I get the same error.</p> <p>Any ideas what I am doing wrong here ?</p> <h2>## Update ##</h2> <p>I guess I spotted why this is happening. The problem is with the BaseItem, which is the base class of all other classes where I have ModifiedUser and CreatedUser. Well I also have a User class which inherits from baseitem. So now I am facing a circular dependency between BaseItem and User. Or maybe in this case since User inherits from BaseItem, we have a Circular Dependency on a self-referencing object.</p> <pre><code>public class User : BaseItem { public int UserId { get; set; } [Required] public string UserName { get; set; } } </code></pre> <p>I tried to make the ModifiedUser and CreatedUser as virtual but I have WCF and it wont serialize.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload