问题
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:
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;
来源:https://stackoverflow.com/questions/58408156/enitity-framework-each-property-name-in-a-type-must-be-unique