代码之家  ›  专栏  ›  技术社区  ›  Sfmar

种子一对一关系ASP.NET MVC

  •  0
  • Sfmar  · 技术社区  · 6 年前

    我正在使用ASP.NETMVC5和EF6构建一个web应用程序。

    我正在尝试将数据播种到一对一关系,定义如下:

    public class Client 
    {
      public int ClientId { get; set; }  
      [Required]
      public string Name { get; set; }
    
      public Address Address { get; set; }
    }
    
    public class Address 
    {
      [ForeignKey("Client")]
      public int AddressId { get; set; }  
      [Required]
      public string StreetName { get; set; }
    
      public Client Client { get; set; }
    }
    

    其中Address是从属端。

    /迁移/Configuration.cs 我添加了以下代码:

    internal sealed class Configuration : DbMigrationsConfiguration<MR_TrackTrace.Models.ApplicationDbContext>
        {
            public Configuration()
            {
                AutomaticMigrationsEnabled = false;
            }
    
            protected override void Seed(MR_TrackTrace.Models.ApplicationDbContext context)
            {
    
                var clients = new List<Client>
                {
                    new Client { Name = "John" },
                    new Client { Name = "Mary" },
                    new Client { Name = "Philip" }
                };
    
                clients.ForEach(s => context.Clients.AddOrUpdate(p => p.Name, s));
    
                context.SaveChanges();
                var addresses = new List<Address>
                {
                    new Address { StreetName = "something", Client = clients.FirstOrDefault(s => s.Name == "John") },
                    new Address { StreetName = "other", Client = clients.FirstOrDefault(s => s.Name == "Philip") },
                    new Address { StreetName = "another", Client = clients.FirstOrDefault(s => s.Name == "Mary") }
                };
    
                addresses.ForEach(s => context.Addresses.AddOrUpdate(p => p.Name, s));
                context.SaveChanges();
            }
     }
    

    但此列没有用预期的ID填充,而是用“0”填充。通过使用:

    Client = clients.FirstOrDefault(s => s.Name == "John")
    

    我希望上下文会自动设置此表的ClientId。

    提前谢谢!

    2 回复  |  直到 6 年前
        1
  •  0
  •   Alex Grogan    6 年前

    您不能添加如下地址:

    var clients = new List<Client>
    {
        new Client { Name = "John", Address = new Address() { StreetName = "Something" } },
        new Client { Name = "Mary", Address = new Address() { StreetName = "Other" }  },
        new Client { Name = "Philip", Address = new Address() { StreetName = "Another" }  }
    };
    

    当它保存更改时,它应该同时创建客户机和地址,并将它们链接在一起

        2
  •  0
  •   Sfmar    6 年前

    通过如下方式解决了我的问题:

     protected override void Seed(MR_TrackTrace.Models.ApplicationDbContext context)
            {
    
                var clients = new List<Client>
                {
                    new Client { Name = "John" },
                    new Client { Name = "Mary" },
                    new Client { Name = "Philip" }
                };
    
                clients.ForEach(s => context.Clients.AddOrUpdate(p => p.Name, s));
    
                context.SaveChanges();
                var addresses = new List<Address>
                {
                    new Address { StreetName = "something", ClientId = context.Clients.FirstOrDefault(s => s.Name == "John").ClientId },
                    new Address { StreetName = "other", ClientId = context.Clients.FirstOrDefault(s => s.Name == "Philip").ClientId },
                    new Address { StreetName = "another", ClientId = context.Clients.FirstOrDefault(s => s.Name == "Mary").ClientId }
                };
    
                addresses.ForEach(s => context.Addresses.AddOrUpdate(p => p.Name, s));
                context.SaveChanges();
            }
    

    仍然不知道为什么EF不能链接这两个表并在Address表中自动填充ClientId。