0

我需要通过掩码生成主键(例如:)BS{100-999}-{Rand(100-999)}但我不知道如何在 EF 代码中生成这样的键/有人可以帮我解决这个问题吗?

我的模型:

[Table("Tickets")]
public class Ticket
{
    public Ticket()
    {
        CreationDate = DateTime.UtcNow;
        LastChangeDate = DateTime.UtcNow;
        UserReviewed = true;
        EmailAlert = true;
    }
    [Key]
    [DatabaseGenerated(System.ComponentModel.DataAnnotations.Schema.DatabaseGeneratedOption.Identity)]
    public Guid Id { get; set; } //now it is Guid But i need this to be string and generated by the above mask
    public string UsersName { get; set; }
    public string Theme { get; set; }
    public string Request { get; set; }
    public DateTime CreationDate { get; set; }
    public string TypeMask { get; set; }
    public string StatusMask { get; set; }
    public int SenderId { get; set; }
    public bool EmailAlert { get; set; }
    public DateTime LastChangeDate { get; set; }
    public bool UserReviewed { get; set; }
    public virtual ICollection<TicketRecord> Answers { get; set; }
    [ForeignKey("StatusMask")]
    public virtual TicketStatus Status { get; set; }
    [ForeignKey("TypeMask")]
    public virtual TicketType Type { get; set; }
    [ForeignKey("SenderId")]
    public virtual UserProfile Sender { get; set; }
    public virtual AttachmentsCollection IncludedFiles { get; set; }
}
4

1 回答 1

0

EF 从不生成密钥。您需要自己生成密钥,或者让 EF 知道密钥将由数据库使用 StoreGeneragedPattern 注释/设置生成。EF 无法生成密钥,因为它不能保证生成值的唯一性。

于 2013-03-19T16:06:15.400 回答