简体   繁体   English

种子一对一关系ASP .NET MVC

[英]Seed One-to-one Relationship ASP .NET MVC

I am building a web application using ASP .NET MVC5 and EF6. 我正在使用ASP .NET MVC5和EF6构建Web应用程序。

I am trying to seed data to a one-to-one-relationship defined as such: 我正在尝试将数据播种到这样定义的一对一关系:

Models 楷模

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

Where Address is the dependent end. 其中地址是从属端。

Now, I want to seed these two tables using EF and in /Migrations/Configuration.cs I added the following code: 现在,我想使用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();
        }
 }

Now, after adding the migration and updating the database, I checked the tables and the Client is built and seeded according but the Address table has a column named ClientId as expected because it is the Foreign Key. 现在,在添加了迁移并更新了数据库之后,我检查了表,并根据该表构建并植入了Client,但是Address表中的列与预期的一样名为ClientId,因为它是外键。

But this column is not filled with the expected ID, instead it is filled with "0". 但是此列未填充期望的ID,而是填充了“ 0”。 By using: 通过使用:

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

I was expecting that the context would automatically set the ClientId for this table. 我期望上下文会自动为此表设置ClientId。

Can anyone help me guiding me through this issue? 谁能帮我解决这个问题? Thanks in advance! 提前致谢!

Are you not able to add the address like this: 您是否无法添加如下地址:

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

When it save changes, it should create the client and address at the same time and link them together 保存更改时,应同时创建客户端和地址并将它们链接在一起

Solved my problem by seeding as follows: 通过播种来解决我的问题,如下所示:

 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();
        }

Still don't know why EF was not able to link the two tables and automatically fill the ClientId in the Address table. 仍然不知道为什么EF无法链接两个表并自动在Address表中填充ClientId。

Thanks! 谢谢!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM