实体框架查找表导航设置

时间:2018-06-22 04:27:59

标签: sql-server entity-framework ef-fluent-api

我一直在尝试使用“状态”字段配置我的模型之一,该字段被实现为数据库中的查找表。

我参考了这篇文章,以获取有关在EF中设置枚举类型的指导: How to create a table corresponding to enum in EF6 Code First?

尝试创建新的RequestForQuote实体时遇到问题。我配置了模型,以便需要Customer并将所有RequestForQuotes初始化为Draft。问题是,EF一直试图插入新的RequestForQuoteStatus(当然会引发重复的ID错误)。

有人可以建议如何正确配置吗?我试图避免先从Db获取实体,也避免在模型中包含外键。

我还应该注意,我并不是真的想在Status模型中放入RequestForQuotes集合,但是也不知道如何正确配置它。

public class RequestForQuote
{
    public RequestForQuote(Customer customer, ProgramManager programManager)
    {
        Customer = customer;
        Status = RequestForQuoteStatusEnum.Draft;
    }
    public int Id { get; set; }
    public Customer Customer { get; set; }
    public virtual RequestForQuoteStatus Status { get; set; }
    public DateTime? CreateDate { get; set; }
    public DateTime? DueDate { get; set; }
    public string Notes { get; set; }
}
public enum RequestForQuoteStatusEnum
{
    Draft,      //1
    Open,       //2
    RequiresApproval, //3
    Approved,   //4
    Submitted,  //5
    Expired     //6
}
public class RequestForQuoteStatus
{
    private RequestForQuoteStatus(RequestForQuoteStatusEnum @enum)
    {
        Id = (int) @enum + 1;//offset arrays start at 0 :]
        Status = @enum.ToString();
    }

    public int Id { get; set; }

    public string Status { get; set; }

    public virtual ICollection<RequestForQuote> RequestForQuotes { get; set; }

    public static implicit operator RequestForQuoteStatus(RequestForQuoteStatusEnum @enum) => new RequestForQuoteStatus(@enum);

    public static implicit operator RequestForQuoteStatusEnum(RequestForQuoteStatus status) =>
        (RequestForQuoteStatusEnum) status.Id;
}

我的流利的API模型构建器:

public class RequestForQuoteBuilder : EntityTypeConfiguration<RequestForQuote>
{
    public RequestForQuoteBuilder()
    {
        HasKey(e => e.Id);
        Property(e => e.Id)
            .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);

        HasRequired(e => e.Customer)
            .WithMany(f => f.RequestForQuotes)
            .Map(k => k.MapKey("CustomerNumber"));

        HasRequired(e => e.Status)
            .WithMany(f => f.RequestForQuotes)
            .Map(k => k.MapKey("RequestForQuoteStatusId"));

        Property(e => e.CreateDate)
            .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Computed);
    }
}
    public RequestForQuoteStatusBuilder()
    {
        HasKey(e => e.Id);
        Property(e => e.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
        Property(e => e.Status).IsRequired();

    }
}

表格:

CREATE TABLE [dbo].[RequestForQuote]
(
    [Id] INT NOT NULL PRIMARY KEY IDENTITY,
    [CustomerId] CHAR(10) NOT NULL,
    [RequestForQuoteStatusId] INT NOT NULL DEFAULT 1,
    [CreateDate] DATETIME NOT NULL DEFAULT GETDATE(),
    [DueDate] DATETIME,
    [Notes] NVARCHAR(MAX) DEFAULT '', 
    CONSTRAINT [FK_RequestForQuote_RequestForQuoteStatus] FOREIGN KEY ([RequestForQuoteStatusId]) REFERENCES [RequestForQuoteStatus]([Id]) 
)
CREATE TABLE [dbo].[RequestForQuoteStatus]
(
    [Id] INT NOT NULL PRIMARY KEY,
    [Status] VARCHAR(20) NOT NULL UNIQUE
)

0 个答案:

没有答案