种子一对一关系ASP .NET MVC

时间:2018-10-23 11:34:32

标签: c# asp.net asp.net-mvc entity-framework entity-framework-6

我正在使用ASP .NET MVC5和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; }
}

地址是从属端。

现在,我想使用EF播种这两个表,并在 /Migrations/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();
        }
 }

现在,在添加了迁移并更新了数据库之后,我检查了表,并根据该表构建并植入了Client,但是Address表有一个按预期名称为ClientId的列,因为它是外键。

但是此列未填充期望的ID,而是填充了“ 0”。通过使用:

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

我期望上下文会自动为此表设置ClientId。

有人可以帮助我指导我解决此问题吗? 预先感谢!

2 个答案:

答案 0 :(得分:0)

您是否无法添加如下地址:

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" }  }
};

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

答案 1 :(得分:0)

通过播种方法解决了我的问题:

 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无法链接两个表并自动在地址表中填充ClientId。

谢谢!