使用 Code First 时关系 table 中缺少字段
Missing fields in relationship table when using Code First
我在 Entity Framework 6.1.2.
中使用 Code First
我有两个实体/主人 table 像这样:
[Table("Foos")]
public class Foo
{
[Key]
public string Id { get; set;}
// More fields
...
}
[Table("Bars")]
public class Bar
{
[Key]
public int Id { get; set;}
// More fields
...
}
我刚刚定义了它们之间的关系 table,如下所示:
// Each foo can have zero or more bars.
[Tables("FooBars")]
public class FooBar
{
[Key]
public int Id { get; set;}
[ForeignKey("Foo")]
public string FooId { get; set;}
public Foo Foo { get; set;}
[ForeignKey("Bar")]
public int BarId { get; set; }
public Bar Bar { get; set;}
// Extra fields in this relationship table
public bool ThisRelationshipGoingGood { get; set;}
// More fields
...
}
然后我更新了 Foos
实体定义,如下所示:
[Table("Foos")]
public class Foo
{
[Key]
public string Id { get; set;}
public ICollection<Bar> Bars { get; set; }
// More fields
...
}
但是当我 运行 PowerShell / Nuget 程序包控制台中的 update-database
commandlet 时,更新后的数据库 FooBars
table 只有以下列:
FooBars
----------
FooId (string)
BarId (int)
它没有 ThisRelationshipGoingGood
字段或我添加到 FooBars
实体的任何其他字段。
如何让 FooBars
table 具有我定义的剩余字段?
你用过add/enable-migration吗??
看看这个教程https://msdn.microsoft.com/en-us/data/jj591621.aspx
希望对您有所帮助。
使用这个:
[Table("Foos")]
public class Foo
{
[Key]
public string Id { get; set;}
[InverseProperty("Foo")]
public ICollection<FooBar> FooBars { get; set; }
// More fields
...
}
[Table("Bars")]
public class Bar
{
[Key]
public int Id { get; set;}
[InverseProperty("Bar")]
public ICollection<FooBar> FooBars { get; set; }
// More fields
...
}
[Tables("FooBars")]
public class FooBar
{
[Key]
public int Id { get; set;}
public string FooId { get; set;}
[ForeignKey("FooId")]
[InverseProperty("FooBars")]
public Foo Foo { get; set;}
public int BarId { get; set; }
[ForeignKey("BarId")]
[InverseProperty("FooBars")]
public Bar Bar { get; set;}
// Extra fields in this relationship table
public bool ThisRelationshipGoingGood { get; set;}
// More fields
...
}
并且在 consol 包管理器中,在更新数据库命令之前,首先添加迁移,然后使用更新数据库命令。
我在 Entity Framework 6.1.2.
中使用 Code First我有两个实体/主人 table 像这样:
[Table("Foos")]
public class Foo
{
[Key]
public string Id { get; set;}
// More fields
...
}
[Table("Bars")]
public class Bar
{
[Key]
public int Id { get; set;}
// More fields
...
}
我刚刚定义了它们之间的关系 table,如下所示:
// Each foo can have zero or more bars.
[Tables("FooBars")]
public class FooBar
{
[Key]
public int Id { get; set;}
[ForeignKey("Foo")]
public string FooId { get; set;}
public Foo Foo { get; set;}
[ForeignKey("Bar")]
public int BarId { get; set; }
public Bar Bar { get; set;}
// Extra fields in this relationship table
public bool ThisRelationshipGoingGood { get; set;}
// More fields
...
}
然后我更新了 Foos
实体定义,如下所示:
[Table("Foos")]
public class Foo
{
[Key]
public string Id { get; set;}
public ICollection<Bar> Bars { get; set; }
// More fields
...
}
但是当我 运行 PowerShell / Nuget 程序包控制台中的 update-database
commandlet 时,更新后的数据库 FooBars
table 只有以下列:
FooBars
----------
FooId (string)
BarId (int)
它没有 ThisRelationshipGoingGood
字段或我添加到 FooBars
实体的任何其他字段。
如何让 FooBars
table 具有我定义的剩余字段?
你用过add/enable-migration吗?? 看看这个教程https://msdn.microsoft.com/en-us/data/jj591621.aspx 希望对您有所帮助。
使用这个:
[Table("Foos")]
public class Foo
{
[Key]
public string Id { get; set;}
[InverseProperty("Foo")]
public ICollection<FooBar> FooBars { get; set; }
// More fields
...
}
[Table("Bars")]
public class Bar
{
[Key]
public int Id { get; set;}
[InverseProperty("Bar")]
public ICollection<FooBar> FooBars { get; set; }
// More fields
...
}
[Tables("FooBars")]
public class FooBar
{
[Key]
public int Id { get; set;}
public string FooId { get; set;}
[ForeignKey("FooId")]
[InverseProperty("FooBars")]
public Foo Foo { get; set;}
public int BarId { get; set; }
[ForeignKey("BarId")]
[InverseProperty("FooBars")]
public Bar Bar { get; set;}
// Extra fields in this relationship table
public bool ThisRelationshipGoingGood { get; set;}
// More fields
...
}
并且在 consol 包管理器中,在更新数据库命令之前,首先添加迁移,然后使用更新数据库命令。