插入实体并分配外键值

时间:2016-12-10 11:50:45

标签: c# sqlite entity-framework-core .net-core

PostEntity

[Table("Posts")]
public class PostEntity
{
        public PostEntity()
        {
            ViewsCount = 0;
            VotesCount = 0;
        }

        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        [Key]
        public int PostId { get; set; }

        [Required]
        public DateTime CreatedAt { get; set; }

        [Required]
        public string Title { get; set; }

        [Required]
        public string Text { get; set; }

        [Required]
        public string Permalink { get; set; }

        [Required]
        public string CreatedByUserId { get; set; }

        [ForeignKey("CreatedByUserId")]
        public UserEntity CreatedByUser { get; set; }

        public List<PostTagEntity> PostTags { get; set; }
        public List<PostVoteEntity> PostVotes { get; set; }
        public List<PostSpamFlagEntity> PostSpamFlags { get; set; }

        public int ViewsCount { get; set; }
        public int VotesCount { get; set; }

        [Required]
        public string PostType { get; set; }

        [Required]
        public string PostState { get; set; }

        public int? CategoryId { get; set; }

        [ForeignKey("CategoryId")]
        public CategoryEntity Category { get; set; }

        public List<ResponseEntity> Responses { get; set; }
}

用户实体

public class UserEntity : IdentityUser
{
        public async Task<IdentityResult> GenerateUserIdentityAsync(UserManager<UserEntity> manager, string authenticationType)
        {
            // Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
            var userIdentity = await manager.CreateAsync(this, authenticationType);
            // Add custom user claims here
            return userIdentity;
        } 

        public String Name { get; set; }
        public String Gender { get; set; }
        public String ProfilePictureUrl { get; set; }
        public String InfoMessage { get; set; }
        public String Country { get; set; }
        public DateTime BirthDate { get; set; }
        public List<PostEntity> Posts { get; set; }
        public string LockoutReason { get; set; }
        public string LockoutAdditionalComment { get; set; }
}

问题:

PostEntity post = new PostEntity();
post.CategoryId = request.CategoryId;
post.CreatedAt = DateTime.UtcNow; 
post.CreatedByUserId = user.Id; // <-- doesn't work
post.PostType = "B";
post.Title = request.Title;
post.PostState = "N";
post.Permalink = Permalink.Create(request.Title);
post.Text = request.Content;

UpdateBlogpostTags(post, request.Tags);

m_context.Posts.Add(post);
m_context.SaveChanges();

我明白了:

  

Microsoft.EntityFrameworkCore.DbUpdateException:更新条目时发生错误。有关详细信息,请参阅内部异常

     

Microsoft.Data.Sqlite.SqliteException:SQLite错误19:&#39; FOREIGN KEY约束失败&#39;。

但如果我改变:

post.CreatedByUserId = user.Id;

为:

post.CreatedByUser = user; 

成功插入。

这也有效,但感觉不对:

post.CreatedByUser = new UserEntity(){
    Id = user.Id
};

我错过了什么?

0 个答案:

没有答案