Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-core/3.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
Asp.net core 在insert-EF Core和Web Api 3.1上创建重复的外键条目_Asp.net Core_Ef Core 3.1 - Fatal编程技术网

Asp.net core 在insert-EF Core和Web Api 3.1上创建重复的外键条目

Asp.net core 在insert-EF Core和Web Api 3.1上创建重复的外键条目,asp.net-core,ef-core-3.1,Asp.net Core,Ef Core 3.1,我目前正在学习如何通过简单的一对多设置使用EF Core,其中用户可以有许多项。就从表中检索数据而言,对于一些DTO模型来说,这很好;然而,当我尝试通过邮递员添加一个包含多个项目的用户时,我注意到,对于每个项目,它已经复制了用户很多次(即一个包含3个项目的用户将创建3个项目和3个用户): 邮递员(邮政) 背景: namespace TestWebApplication.Database { public class MyContext : DbContext {

我目前正在学习如何通过简单的一对多设置使用EF Core,其中
用户
可以有许多
。就从表中检索数据而言,对于一些
DTO
模型来说,这很好;然而,当我尝试通过邮递员添加一个包含多个
项目的
用户时,我注意到,对于每个
项目
,它已经复制了用户很多次(即一个包含3个
项目
用户
将创建3个
项目
和3个
用户
):

邮递员(邮政)

背景:

namespace TestWebApplication.Database
{
    public class MyContext : DbContext
    {
        public DbSet<User> Users { get; set; }
        public DbSet<Items> Items { get; set; }

        public MyContext(DbContextOptions<MyContext> options)
            : base(options)
        {
            // erm, nothing here
        }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<User>()
                .HasMany(i => i.Items)
                .WithOne(u => u.User);
        }

        public override int SaveChanges()
        {
            var entities = from e in ChangeTracker.Entries()
                where e.State == EntityState.Added
                      || e.State == EntityState.Modified
                select e.Entity;

            foreach (var entity in entities)
            {
                var validationContext = new ValidationContext(entity);
                Validator.ValidateObject(entity, validationContext);
            }

            return base.SaveChanges();
        }
    }
}
项目:

namespace TestWebApplication.Database
{
    public class Items
    {
        [Key]
        public int ItemId { get; set; }
        public string Item { get; set; }
        public string Category { get; set; }
        public string Type { get; set; }
        public virtual User User { get; set; }
    }
}
用户:

namespace TestWebApplication.Database
{
    public class User
    {
        [Key]
        public int UserId { get; set; }
        public string UserName { get; set; }
        public string Dob { get; set; }
        public string Location { get; set; }
        public ICollection<Items> Items { get; set; }
    }
}
namespace TestWebApplication.Database
{
公共类用户
{
[关键]
public int UserId{get;set;}
公共字符串用户名{get;set;}
公共字符串Dob{get;set;}
公共字符串位置{get;set;}
公共ICollection项{get;set;}
}
}

我重新查看了我的代码,并将我的
Items.cs
模型更改为:

namespace TestWebApplication.Database
{
    public class Items
    {
        [Key]
        public int ItemId { get; set; }
        public string Item { get; set; }
        public string Category { get; set; }
        public string Type { get; set; }

        [ForeignKey("User")] // added this
        public int? UserId { get; set; } // added this

        public virtual User User { get; set; }
    }
}
现在,当我通过邮递员发送上面的
json
时,我得到了多个项目,它们的用户与外键相同

下面的网站似乎有所帮助:


您是否有一些特定的用例需要为
SaveChanges
方法编写自己的覆盖?从你的例子中,我不认为有必要覆盖它,我认为这只是让事情变得过于复杂。如果您想要一个简单的示例,可以添加一个包含多个项的用户,那么我可以提供一个。我尝试了您的代码,它只创建了一个包含三个项的用户。请确保项中的外键正确。@XingZou您的意思是包含外键
public int userid{get;set;}
以及
项目
模型中的
公共虚拟用户用户{get;set;}
?我的代码中确实有这样的内容,但是在检索数据时没有任何区别,所以我把它去掉了。好吧,也许你可以尝试一个新的mvc项目,或者提供一个可复制的演示。
namespace TestWebApplication.Database
{
    public class User
    {
        [Key]
        public int UserId { get; set; }
        public string UserName { get; set; }
        public string Dob { get; set; }
        public string Location { get; set; }
        public ICollection<Items> Items { get; set; }
    }
}
namespace TestWebApplication.Database
{
    public class Items
    {
        [Key]
        public int ItemId { get; set; }
        public string Item { get; set; }
        public string Category { get; set; }
        public string Type { get; set; }

        [ForeignKey("User")] // added this
        public int? UserId { get; set; } // added this

        public virtual User User { get; set; }
    }
}