Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 添加实体对象及其基于主键的相关实体_C#_Entity Framework_Asp.net Core - Fatal编程技术网

C# 添加实体对象及其基于主键的相关实体

C# 添加实体对象及其基于主键的相关实体,c#,entity-framework,asp.net-core,C#,Entity Framework,Asp.net Core,我有这样一个实体: public class Post { public int Id { get; set; } public string Title { get; set; } public string FullText { get; set; } public string Tags { get; set; } public virtual Category Category { get; set; } } var post = new Pos

我有这样一个实体:

public class Post
{
    public int Id { get; set; }
    public string Title { get; set; }
    public string FullText { get; set; }
    public string Tags { get; set; }
    public virtual Category Category { get; set; }
}
var post = new Post
{
    Category = new Category { Id = model.CategoryId },
    FullText = model.FullText,
    Tags = model.Tags,
    Title = model.Title,
};
_articsleService.Save(post);
此实体与
类别有关系

public class Category
{
    public int Id { get; set; }
    public string Name { get; set; }
    public virtual IList<Post> Articles { get; set; }
}
这段代码工作正常,但它有1个从数据库中提取数据的功能。在具有3个以上关系的实体对象中,我认为这种方式不太好。因此,另一种方式可以如下所示:

public class Post
{
    public int Id { get; set; }
    public string Title { get; set; }
    public string FullText { get; set; }
    public string Tags { get; set; }
    public virtual Category Category { get; set; }
}
var post = new Post
{
    Category = new Category { Id = model.CategoryId },
    FullText = model.FullText,
    Tags = model.Tags,
    Title = model.Title,
};
_articsleService.Save(post);
但是,当代码运行时,会出现以下异常:

当identity\u insert设置为OFF时,无法在表“Categories”中为identity列插入显式值


是否有解决此问题的最佳方案?

将FK to类别添加到您的后期模型中

public class Post
{
    public int Id { get; set; }
    public string Title { get; set; }
    public string FullText { get; set; }
    public string Tags { get; set; }
    public virtual Category Category { get; set; }
    public int CategoryId { get; set; }
}
然后在更新数据库模型后添加一篇新文章

var post = new Post
{
    CategoryId = model.CategoryId,
    FullText = model.FullText,
    Tags = model.Tags,
    Title = model.Title,
};
_articsleService.Save(post);
就这样,EF将为您完成其余的工作

您当前的方法导致创建一个新类别,但该类别不起作用,因为已经存在相同的主键,并且默认情况下不会启用“插入标识列”。这就是EF的变更跟踪工作原理。EF从每个数据库行创建一个代理作为代码中的实体,并在应用程序生命周期内跟踪该实体


您可以阅读更多信息。

将FK to类别添加到您的后期模型中

public class Post
{
    public int Id { get; set; }
    public string Title { get; set; }
    public string FullText { get; set; }
    public string Tags { get; set; }
    public virtual Category Category { get; set; }
    public int CategoryId { get; set; }
}
然后在更新数据库模型后添加一篇新文章

var post = new Post
{
    CategoryId = model.CategoryId,
    FullText = model.FullText,
    Tags = model.Tags,
    Title = model.Title,
};
_articsleService.Save(post);
就这样,EF将为您完成其余的工作

您当前的方法导致创建一个新类别,但该类别不起作用,因为已经存在相同的主键,并且默认情况下不会启用“插入标识列”。这就是EF的变更跟踪工作原理。EF从每个数据库行创建一个代理作为代码中的实体,并在应用程序生命周期内跟踪该实体

您可以阅读更多信息。

当使用“Category=new Category”时,您正在创建一个全新的实体,即使您将其ID设置为数据库中的现有ID,ORM仍将其作为一个新实体管理以存储在数据库中。当使用“Category=new Category”时,您正在创建一个全新的实体,即使将其ID设置为数据库中的现有ID,ORM仍将其作为新实体进行管理,以存储在数据库中