0

I have the following entities defined and the relationship mapping also. However, while saving i get the error Name: Each property name in a type must be unique. Property name 'ResponseId' is already defined. in EF 6

System.Data.Entity.ModelConfiguration.ModelValidationException

I am struck with this for 2 days and not sure, what is the issue in my class. If I remove, ResponseId in ResponseOffer, it is working fine. However, I need to set this value to save it in the database and its FK

Request may contain many responses and each response may contain many response offer.

[Table("Response")]
public class Response : AggregateRoot // AggregateRoot is an Entity
{
    public Response(Request Request): this()
    {
        ResponseId = Guid.NewGuid();
        Request = Request;
        MaterialId = Request.MaterialId;           
    }

    public Response()
    {
    }

    public Response(Guid ResponseId)
    {
        ResponseId = ResponseId;
    }

    public Response(Guid ResponseId, Request Request)
    {
        ResponseId = ResponseId;
        Request = Request;            
    }

    public Guid ResponseId { get; private set; }     
    public virtual Request Request { get; set; }
    public bool IsActiveVersion { get; set; }
    public EntityHashSet<ResponseOffer> EditableResponseOffers => (EntityHashSet<ResponseOffer>)ResponseOffers;

}

Then

[Table("ResponseOffer")]
public class ResponseOffer : Entity
{
    public ResponseOffer()
    {
    }
    public ResponseOffer(Response response)
    {
        ResponseOfferId = Guid.NewGuid();
        Response = response;            
    }

    public Guid ResponseOfferId { get; set; }
    public string RequestItemId { get; set; }
    public Guid ResponseId { get; set; }
    public decimal Price { get; set; }
    public decimal Quantity { get; set; }      
    public string Comment { get; set; }
    public virtual Response Response { get; set; }
}

Mapping

    modelBuilder.Entity<Response>()
            .HasRequired(e => e.Request)
            .WithMany(e => e.Responses)
            .WillCascadeOnDelete(false);         

        modelBuilder.Entity<Response>()
            .HasMany(e => e.ResponseOffers)
            .WithRequired(e => e.Response)
            .WillCascadeOnDelete(false);

UPDATE:

I have removed the following properties and now I dont get the same error. However EF adds extra column Response_ResponseId1 while insert

INSERT [dbo].[ResponseOffer]([ResponseOfferId],  [RequestItemId], 
[Price], [Quantity], [Comment], [ResponseId], [Response_ResponseId1])
Developer
  • 487
  • 9
  • 28
  • Isn't the `Response` an `Entity`? You didn't specify the inheritance. – Palle Due Oct 16 '19 at 07:42
  • @PalleDue yes, just updated it. Same issue still – Developer Oct 16 '19 at 07:45
  • EF6 or EF Core? – vernou Oct 16 '19 at 07:50
  • Can we see the class Entity and AggregateRoot? – vernou Oct 16 '19 at 08:00
  • @Orwel those classes has only a simple properties like AddedDate and IsActive properties – Developer Oct 16 '19 at 08:04
  • I can't reproduce with just the code present. You can try remove some properties and modelBuilder calls. – vernou Oct 16 '19 at 08:35
  • Have you tried explicitly setting the FK using the fluent API .HasForeignKey in your modelBuilder configurations? The key that is being defined just looked auto-named to me like EF is confused on what you want. Normally you'd either use Fluent API as described, or a data annotation. It seems naming convention might be failing you in this case. – kd345205 Oct 16 '19 at 12:53
  • @kd345205 Yes I did HasForeignKey. Still did not work – Developer Oct 16 '19 at 12:59
  • can you post the missing pieces? are there properties in aggregate root? What is in the most basic version of the request class? Response does not have a collection of responseoffer in your definition. I think it would help to get the extra information on this one. Please paste exact code if you can, what you've provided cant compile due to naming issues that need fixed before you can do anything. – kd345205 Oct 16 '19 at 13:07

1 Answers1

0

I'm not sure, but, I would try to set with virtual the "EditableResponseOffers" property and test the behavior with removing the private to ResponseId.

public Guid ResponseId { get; set; }
public virtual EntityHashSet<ResponseOffer> EditableResponseOffers => (EntityHashSet<ResponseOffer>)ResponseOffers;
devGirl
  • 58
  • 4