开发者

Multiplicity error when generating database from model?

have 3 classes:

     public class User
    {
        public 开发者_C百科Guid Id { get; set; }
        public string Username { get; set; }
        public string PersonelNumber { get; set; }
        public string Password { get; set; }
        public bool IsActive { get; set; }
        public DateTime LastLoginDate { get; set; }
        public bool IsOnline { get; set; }
    }

    public class Comment
    {
        public Guid Id { get; set; }

        /// <summary>
        /// Title of the comment
        /// </summary>
        public string Title { get; set; }

        /// <summary>
        /// Full text for comment
        /// </summary>
        public string Text { get; set; }

        //Maps to the User Id
        public Guid CreatedById { get; set; }

        public virtual User CreatedBy { get; set; }
    }

    public class FileVersion 
    {
        public Guid Id { get; set; }
        public string Path { get; set; }
        public int Version { get; set; }

//Referecne to optional comment        
        public virtual Guid ChangeCommentId { get; set; }
        public virtual Comment ChangeComment { get; set; }
//Referece to user who added a file
        public virtual Guid AddedById { get; set; }
        public virtual User AdddedBy { get; set; }
//Reference to User who may have the file checked out
        public virtual Guid CheckedOutById { get; set; }
        public virtual User CheckedOutBy { get; set; }
}

I am receiving the error when the database tries to create the Comment relationship between FilVersion and Comment. I did create this mapping for the FileVersion class:

HasOptional(f => f.ChangeComment).WithMany().HasForeignKey(f => f.ChangeCommentId).WillCascadeOnDelete(false);

I am not sure how to setup my mappings so that I do not get the Multiplicity error when the database is created. The FileVersion needs a referecne to who added the file and who may have it checkedout for edit.


The problem is that you cannot define Comment as optional when the foreign key doesn't accept null. Either modify your entity to:

public class FileVersion 
{
    ...     
    public virtual Guid? ChangeCommentId { get; set; }
    public virtual Comment ChangeComment { get; set; }
    ...      
}

Or modify your mapping to:

modelBuilder.Entity<FileVersion>()
            .HasRequired(f => f.ChangeComment)
            .WithMany()
            .HasForeignKey(f => f.ChangeCommentId)
            .WillCascadeOnDelete(false);

Same will be probably needed for CheckedOutBy

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜