【发布时间】:2020-10-12 10:23:41
【问题描述】:
enter code here
[Table("Articles")]
public class Article : IEntity
{
[Required, StringLength(60)]
public string Title { get; set; }
public int CategoryId { get; set; }
[Required, StringLength(2500)]
public string Text { get; set; }
public int LinkCount { get; set; }
public virtual User Owner { get; set; }
public virtual Category Category { get; set; }
public virtual Html_Content_Result HtmlPage { get; set; }
public virtual List<Comment> Comments { get; set; }
public virtual List<Liked> Likes { get; set; }
}
[Table("Users")]
public class User : IEntity
{
[StringLength(25)]
public string Name { get; set; }
[StringLength(25)]
public string Lastname { get; set; }
[StringLength(25), Required]
public string Username { get; set; }
[StringLength(100), Required]
public string Password { get; set; }
[StringLength(70), Required]
public string Email { get; set; }
public bool IsActive { get; set; }
[Required]
public Guid ActivateGuid { get; set; }
public virtual List<Article> Articles { get; set; }
public virtual List<Comment> Comments { get; set; }
public virtual List<UsersRole> UsersRoles { get; set; }
public virtual List<Liked> Likes { get; set; }
}
我有两个名为 User 和 Article 的实体。 我首先使用实体框架代码创建了我的数据库。 这两个表之间存在一对多的关系。 问题是当我向文章表插入操作时,我从会话添加到模型用户实体并在我的用户表上插入重复的输入,因为我之前已经插入了我的用户。
我应该怎么做才能解决?
【问题讨论】:
-
请添加产生重复的插入代码。
-
当然,我在下面加了。
-
1) 请将其添加到问题 2) 请添加适用于实体框架的代码
标签: database entity-framework ef-code-first code-first relational