0

I have following structure:

Class A{

    Public virtual int Id{get;set;}
    [Required]
    public virtual string Title { get; set; }
    [Required]
    public virtual string Body { get; set; }
}

Class B{

   Public virtual int Id{get;set;}
   [Required]
   public virtual string Description{ get; set; }
}

Class C{

  public virtual int Id{get;set;}
  public virtual int AId {get;set;}
  public virtual A A{get;set;}
  public virtual int BId {get;set;}
  public virtual B B{get;set;}
}

EntityFramework automatically creates tables in database and add references as well.

Now on saving data in Class C its giving me Validation failed for one or more entities

I have checked System.Data.Entity.Validation.DbEntityValidationException

and found that its giving error Title and Body is required. Why I need to pass Title and Body if I am passing AId and BId. and also I have checked if I pass Title and Body then it saves the data in C as well as A and B. I just want to save only in C.

Can you please guide what I am doing wrong.

Thanks in advance.

Marco
  • 22,856
  • 9
  • 75
  • 124
Anuj
  • 1
  • 1
  • possible duplicate of [Validation failed for one or more entities. See 'EntityValidationErrors' property for more details](http://stackoverflow.com/questions/7795300/validation-failed-for-one-or-more-entities-see-entityvalidationerrors-propert) – Mohammad Sepahvand Mar 03 '14 at 06:53
  • Why do you mark everything as `virtual` ? – Marco Mar 03 '14 at 07:03

1 Answers1

0

Please post the sample code that actually creates and saves the objects.

It's likely because A and B are properties of C. In order to save C, the EF needs first to save A and B and link them to C in a foreign key relationship. You can't have a C without having an A and B and you can't save A without Title and Body as they are required fields.

Without seeing more code it's hard to say for sure.

Simon
  • 6,062
  • 13
  • 60
  • 97