Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/83.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
C# 在迁移中编写更新sql脚本_C#_Sql_Asp.net_Asp.net Mvc_Entity Framework - Fatal编程技术网

C# 在迁移中编写更新sql脚本

C# 在迁移中编写更新sql脚本,c#,sql,asp.net,asp.net-mvc,entity-framework,C#,Sql,Asp.net,Asp.net Mvc,Entity Framework,我有一个业主的实体。 模型如下: [Required] [MaxLength(PM101Consts.MaxNameLength)] public virtual string FirstName { get; set; } [Required] [MaxLength(PM101Consts.MaxNameLength)] public virtual string LastName { get; set; } [MaxLength(PM101C

我有一个业主的实体。 模型如下:

[Required]
    [MaxLength(PM101Consts.MaxNameLength)]
    public virtual string FirstName { get; set; }
    [Required]
    [MaxLength(PM101Consts.MaxNameLength)]
    public virtual string LastName { get; set; }

    [MaxLength(PM101Consts.MaxCompanyNumberLength)]
    public virtual string CompanyNumber { get; set; }

    [MaxLength(PM101Consts.MaxVatNumberLength)]
    public virtual string VatNumber { get; set; }

    [Required]
    public virtual int LandlordTypeId { get; set; }

    public string Email { get; set; }

    [Required]
    public virtual Status Status { get; set; }

    [Required]
    [MaxLength(PM101Consts.MaxCompanyNameLength)]
    public virtual string OrganizationName { get; set; }

    [MaxLength(PM101Consts.MaxWebsiteLength)]
    public virtual string Website { get; set; }

    public long? UserId { get; set; }

    [ForeignKey("UserId")]
    public User User { get; set; }
我使用以下属性创建用户表:

public virtual string Name { get; set; }

public virtual string Surname { get; set; }

public virtual string Email { get; set; }

public virtual string Password { get; set; }
我需要为数据库中已经创建的所有房东创建用户。我需要在迁移中这样做。所以User.Name=lown.FirstName,User.Name=lown.LastName,
User.Email=lown.Email。如何编写脚本。

您可以像下面的示例一样为数据种子

protected override void Seed(DbContext context)
{
   var landlords = context.Landlord.ToList();

   foreach(var landlord in loandlords){
     var user = new Users(){
         Name = landlord.FirstName,
         Surname = landlord.LastName,
         Email = landlord.Email
   }
   context.Users.Add(user); 
}

    base.Seed(context);
}


可以我可以在迁移中运行此种子吗?我想最好是让更新脚本在你运行应用程序时运行,或者你可以使用这个sql脚本``插入用户名、姓氏、电子邮件选择FirstName作为'Name',LastName作为'LastName',来自房东的电子邮件```