Enitity Framework Each property name in a type must be unique

c# ef-model-builder entity-framework entity-framework-6

Question

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])
1
0
10/16/2019 12:24:57 PM

Popular Answer

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;
0
10/16/2019 12:50:29 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow