【问题标题】:Seed One-to-one Relationship ASP .NET MVC种子一对一关系 ASP .NET MVC
【发布时间】:2018-10-23 11:34:31
【问题描述】:

我正在使用 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();
        }
 }

现在,在添加迁移和更新数据库之后,我检查了表,客户端是根据构建和播种的,但是地址表有一个名为 ClientId 的列,因为它是外键。

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

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

我希望上下文会自动为该表设置 ClientId。

谁能帮我指导我解决这个问题? 提前致谢!

【问题讨论】:

  • 你试过用 [Key] 装饰你的 clientId 属性吗?
  • @federicoscamuzzi 你的意思是,在客户端模型中给ClientId添加[Key]注解?
  • 是 public int ClientId { get;放; } 添加[键]
  • 你在哪里告诉 ClientId 是你的主键?
  • @federicoscamuzzi 刚刚尝试了您的建议,但不幸的是,没有任何变化。

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


【解决方案1】:

通过播种解决了我的问题,如下所示:

 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 填充到 Address 表中。

谢谢!

【讨论】:

    【解决方案2】:

    你不能这样添加地址吗:

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

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

    【讨论】:

    • 刚刚尝试了您的解决方案,但地址表中的 ClientId 列仍然填充为“0”。在这种情况下,我应该在哪里为地址执行 AddOrUpdate?在执行 clients.ForEach(s => context.Clients.AddOrUpdate(p => p.Name, s)); 时是否隐含? ?
    • 我唯一的其他建议就像他们上面所说的那样。将 [ForeignKey("Client")] 更改为 [Key, ForeignKey("Client")]
    • 刚刚意识到,根据您的操作方式,地址表中不应该有您想要的“ClientId”列,以便地址表中的 AddressId 与 ClientId 中的相同客户表
    • 我不确定我是否做得正确,但我打算将地址与客户相关联。 Client 表可以在没有 Address 的情况下存在,但 Address 必须与 Client 相关联。这就是为什么在创建 POCO 类时,正如我所读到的,外键应该在依赖实体(在本例中为地址)上声明。并且 EF 会自动在 Address 表中创建 ClientId 列。
    • 您是否尝试更改为 [Key, ForeignKey("Client")] 然后创建新的迁移?如果它不起作用,我没有想法对不起
    猜你喜欢
    • 2016-02-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-24
    • 2021-09-15
    • 2022-11-14
    • 2015-12-23
    • 2017-11-11
    相关资源
    最近更新 更多