0

我定义了以下实体以及关系映射。但是,在保存时我得到了Name: Each property name in a type must be unique. Property name 'ResponseId' is already defined.EF 6中的错误

System.Data.Entity.ModelConfiguration.ModelValidationException

我对此感到震惊 2 天,不确定,我班上的问题是什么。如果我删除 ResponseOffer 中的 ResponseId,它工作正常。但是,我需要设置此值以将其保存在数据库及其 FK 中

请求可能包含许多响应,每个响应可能包含许多响应提议。

[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;

}

然后

[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; }
}

映射

    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);

更新:

我已经删除了以下属性,现在我没有收到同样的错误。但是 EF在插入时添加了额外的列Response_ResponseId1

INSERT [dbo].[ResponseOffer]([ResponseOfferId],  [RequestItemId], 
[Price], [Quantity], [Comment], [ResponseId], [Response_ResponseId1])
4

1 回答 1

0

我不确定,但是,我会尝试使用虚拟设置“EditableResponseOffers”属性并通过删除 ResponseId 的私有来测试行为。

public Guid ResponseId { get; set; }
public virtual EntityHashSet<ResponseOffer> EditableResponseOffers => (EntityHashSet<ResponseOffer>)ResponseOffers;
于 2019-10-16T12:50:29.517 回答