我想在Yii2中通过迁移添加一个新列,使用以下代码:
public function up()
{
$this->addColumn('news', 'priority', $this->integer());
}
public function down()
{
$this->dropColumn('news', 'priority');
}
它有效,但我希望它在名字之后成为第二列。
有可能吗?
答案 0 :(得分:3)
好的,你可以试试这个:
public class OracleContext : DbContext
{
public virtual DbSet<Parent> Parents { get; set; }
public virtual DbSet<Child> Children { get; set; }
public virtual DbSet<Reference> References { get; set; }
public virtual DbSet<SomethingElse> SomethingElses { get; set; }
}
public class Parent
{
[Key, MaxLength(30)]
public string Name { get; set; }
[InverseProperty("Parent")]
public virtual List<Child> Children { get; set; } = new List<Child>();
}
public class Child
{
[Key, Column(Order = 1), MaxLength(30)]
public string Parent_Name { get; set; }
[Key, Column(Order = 2), MaxLength(30)]
public string Name { get; set; }
public string Reference_Name { get; set; }
[ForeignKey("Parent_Name")]
public virtual Parent Parent { get; set; }
[ForeignKey("Reference_Name")]
public virtual Reference Reference { get; set; }
public Child Clone()
{
return new Child
{
Parent_Name = this.Parent_Name,
Name = this.Name,
Reference_Name = this.Reference_Name,
Parent = this.Parent,
Reference = this.Reference
};
}
}
public class Reference
{
[Key, MaxLength(30)]
public string Name { get; set; }
}
public class SomethingElse
{
[Key, MaxLength(30)]
public string Name { get; set; }
}
private void button1_Click(object sender, EventArgs e)
{
OracleContext context = new OracleContext();
Reference reference = context.References.Add(new Reference { Name = "Reference" });
Parent alpha = context.Parents.Add(new Parent { Name = "Alpha" });
Child alphaOne = new Child { Name = "AlphaOne" };
Child alphatwo = new Child { Name = "AlphaTwo", Reference_Name = "Reference" };
alpha.Children.AddRange(new List<Child> { alphaOne, alphatwo });
alphaOne.Reference = reference;
var list = (
from child in alpha.Children
select new
{
Time = "Before referencing SomethingElses.Local",
Child = child.Clone()
}
).ToList();
var x = context.SomethingElses.Local;
list.AddRange(
from child in alpha.Children
select new
{
Time = "After referencing SomethingElses.Local",
Child = child.Clone()
}
);
list.AddRange(
from parent in context.Parents.Local
from child in parent.Children
select new
{
Time = "Before SaveChanges",
Child = child.Clone()
}
);
context.SaveChanges();
list.AddRange(
from parent in context.Parents.Local
from child in parent.Children
select new
{
Time = "After SaveChanges",
Child = child.Clone()
}
);
foreach (var item in list)
{
Console.WriteLine("{0}:\r\n\tName = '{1}'\r\n\tParent = '{2}' ({3})\r\n\tReference = '{4}' ({5})",
item.Time, item.Child.Name, item.Child.Parent_Name, item.Child.Parent, item.Child.Reference_Name, item.Child.Reference);
}
}
Before referencing SomethingElses.Local:
Name = 'AlphaOne'
Parent = '' ()
Reference = '' (WindowsFormsApplication2.Reference)
Before referencing SomethingElses.Local:
Name = 'AlphaTwo'
Parent = '' ()
Reference = 'Reference' ()
After referencing SomethingElses.Local:
Name = 'AlphaOne'
Parent = 'Alpha' (WindowsFormsApplication2.Parent)
Reference = '' (WindowsFormsApplication2.Reference)
After referencing SomethingElses.Local:
Name = 'AlphaTwo'
Parent = 'Alpha' (WindowsFormsApplication2.Parent)
Reference = 'Reference' ()
Before SaveChanges:
Name = 'AlphaOne'
Parent = 'Alpha' (WindowsFormsApplication2.Parent)
Reference = '' (WindowsFormsApplication2.Reference)
Before SaveChanges:
Name = 'AlphaTwo'
Parent = 'Alpha' (WindowsFormsApplication2.Parent)
Reference = 'Reference' ()
After SaveChanges:
Name = 'AlphaOne'
Parent = 'Alpha' (WindowsFormsApplication2.Parent)
Reference = 'Reference' (WindowsFormsApplication2.Reference)
After SaveChanges:
Name = 'AlphaTwo'
Parent = 'Alpha' (WindowsFormsApplication2.Parent)
Reference = 'Reference' (WindowsFormsApplication2.Reference)
答案 1 :(得分:3)
从v2.0.8开始,您还可以执行以下操作:
$this->addColumn('news', 'priority', $this->integer()->after('name'));
请参见https://github.com/yiisoft/yii2/blob/2.0.8/framework/db/ColumnSchemaBuilder.php#L209